4 min read

Integrate your Drupal site with the Vimeo API using Media

Many Drupal developers are familiar with the Media module. It is a very powerful and extensible module for managing media files. It also makes life easier for content managers (or more complex for developers). In most cases, we use the Media Vimeo module along with Media. This module allows adding Vimeo videos as media objects on the site. These two modules work well, but we encountered a scenario where this was not enough.

The main idea is to upload videos directly to Vimeo from the site interface and avoid storing video files in the site’s local file system. In this case, we only need to save the video link. Of course, content managers can do this manually in two steps: (1) manually upload the video to Vimeo, and (2) add the link on the site. However, this is not as convenient as it sounds. As a result, the Drupal module Media Vimeo Uploader was created. In this post, I want to explain how this module works and its key features.

Technical side

The Vimeo service provides an excellent API and supports interaction via REST and HTTP requests. We can perform CRUD operations on videos and utilize other REST features. Vimeo also provides a great PHP library, Vimeo PHP Library, which we use in our module.

Technically, we use the Media API, Vimeo PHP library, and the Drupal API. Below are the implementation details:

Step 1: Create a plugin for Media Browser

First, we need to create a new tab in the Media browser. We use hook_media_browser_plugin_info to do that.

/**
 * 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;
}

Next, we create the class MediaVimeoUploaderBrowser, which implements the necessary methods:

class MediaVimeoUploaderBrowser extends \MediaBrowserPlugin {

  public function access($account = NULL) {
    return media_internet_access($account);
  }

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

Step 2: Upload video to Vimeo using cross-origin resource sharing

To upload video files directly to Vimeo, we must override the default #action of the form. Using the Vimeo PHP library, we can get the upload link:

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

This sends the video directly to Vimeo instead of using a custom form handler.

After the form is submitted, the first request goes to Vimeo. Then, Vimeo redirects the user to a specific URL (see 'redirect_url' above), with parameters such as video_id, video_url, and other metadata. We can then use video_url to create a new file object in Drupal and attach it to a node:

$file = media_internet_get_provider($video_url)->save();
file_entity_add_upload_submit($form, $form_state);

// Or manually:
$node->field_video[LANGUAGE_NONE][0]['fid'] = $file->fid;

How the process looks to the user

In the Media Browser, we now have a separate tab called “Vimeo”. It displays a form with a modified #action pointing to the Vimeo service. Once the user submits the form, the video is uploaded and processed on Vimeo, then becomes available.

Conclusion

As a result, we created a new Drupal module, Media Vimeo Uploader, which allows uploading videos directly to Vimeo. All you need is to create a Vimeo Application and provide authentication credentials. Setup instructions are available on drupal.org, where you can configure the Media Vimeo Uploader, your Vimeo app, and authentication settings. If you are interested in the technical details, check the module’s source code or ask questions.