Skip to main content

Integrate your Drupal site with Vimeo API using Media

Published 7.01.2016 Tags: drupal 7

A lot of Drupal Developers familiar with Media module. It’s powerful and extendable framework for file management. Also Media module makes life easier for content managers and harder for developers. Coupled with Media in many cases we're using Media Vimeo module. It gives us ability to add Vimeo videos on our sites as multimedia objects. These two modules are doing nice job, but we have faced with use case, that was't covered.

The main idea - upload video directly to Vimeo and avoid saving file on local file system. In this case we should save only link to video. Of course, content managers can do it manually in two steps: 1st - manually upload video to Vimeo site, 2nd - save link to video on site. In reality it was't so handy for content managers and as result was born Drupal module Media Vimeo Uploader. In this short note I would like to give you some background about Media Vimeo Uploader module and explain some basic functions.

Technical side

Vimeo has great API and it gives us ability to interact with Vimeo REST interface via http requests. In the same fashion it allows us to do CRUD operations with video and together with this it opens a lot of other REST endpoints. Equally important that Vimeo provides great Vimeo PHP library that we are using in our module.

From technical perspective we’re using Media API, Vimeo PHP library and usual Drupal API. Following list indicates high-level technical details how it was implemented:

Step 1: Create Media browser plugin

In the first place to create new tab in Media browser, we’re using hook_media_browser_plugin_info that allows to define new Media plugin.

/**
 * Implements hook_media_browser_plugin_info().
 */
function media_vimeo_uploader_media_browser_plugin_info() {
  $info = array();

  $info['vimeo_uploader'] = array(
    'title' => t('Vimeo'),
    'class' => 'MediaVimeoUploaderBrowser',
  );

  return $info;
}

Then we’ve created appropriate class 'MediaVimeoUploaderBrowser' that implements some necessary methods.

class MediaVimeoUploaderBrowser extends \MediaBrowserPlugin {

  /**
   * {@inheritdoc}
   */
  public function access($account = NULL) {
    return media_internet_access($account);
  }

  /**
   * {@inheritdoc}
   */
  public function view() {
    // Return upload form.

    return array(
      'form' => drupal_get_form('media_vimeo_uploader_upload_form', 1, $options),
    );
  }
}

Step 2: Send Video to vimeo using “Cross-origin resource sharing”

In order to send video file directly to Vimeo service, we should override default #action for form. As I mentioned, we’re using PHP Vimeo library that provides methods to get secure upload link.

// Get upload link for form action.
$response = $lib->request('/me/videos', array(
  'type' => 'POST',
  'redirect_url' => $base_root . request_uri(),
  'link' => '',
), 'POST');
$form['#action'] = $response['body']['upload_link_secure'];

As result video will be sent to Vimeo, instead of using custom submit handler.

Step 3: Save link for Vimeo video

In addition, when user submits this form, first request goes to Vimeo service, but then Vimeo redirects user to predefined url(see 'redirect_url' above), with video_id, video_url and other important information. Therefore we can use video_url to create new file entity and attach it to node.

$file = media_internet_get_provider($video_url)->save();
file_entity_add_upload_submit($form, $form_state);
// or if you are using custom code.
$node->field_video[LANGUAGE_NONE][0]['fid'] = $file->fid;

Front-end side

Consequently on the front-end we have separate tab in Media browser with label “Vimeo”. There we output our form with replaced form #action that leads to Vimeo service. Once user has submitted video, it will be transcoded and available on Vimeo site. For more details see short screencast below.

Conclusion

As result we’ve created contrib module Media Vimeo Uploader that gives you ability to upload video directly to Vimeo out of the box. Everything that you need - create Vimeo Application and provide app credentials. On drupal.org available instructions how you can configure Media Vimeo Uploader, your own app on Vimeo and specify necessary credentials. In case if you need more deep technical details, please take a look at the source code and ask questions if you have.

Comments

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • To post pieces of code, surround them with <code>...</code> tags.

Blog category

  • Drupal 7 (13)
  • Drupal 8 (1)
  • Linux (1)
  • JavaScript (1)
  • Events (12)

Blog archive

  • December 2017 (1)
  • October 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (1)
  • September 2016 (2)
  • June 2016 (1)
  • January 2016 (1)
  • September 2015 (2)
  • May 2015 (1)
  • March 2015 (1)
  • February 2015 (2)
  • November 2014 (1)
  • October 2014 (1)
  • September 2014 (1)
  • July 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • August 2013 (1)
  • June 2013 (1)
  • May 2013 (1)
  • March 2013 (1)
  • July 2012 (1)
  • February 2011 (2)

Contact Author

Alex Schedrov Twitter Icon Alex Schedrov Facebook Icon Alex Schedrov Drupal Icon Alex Schedrov Github Alex Schedrov RSS Icon
© Schedrov Alexander, 2011—2021