Uploading Drupal Node Data to Brightcove

The Brightcove Video Cloud is a popular solution for hosting and publishing videos.

Bill Renk
#Drupal | Posted

The Brightcove Video Cloud is a popular solution for hosting and publishing videos. For developers, Brightcove provides detailed documentation and examples for their API, which can be interfaced with using Javascript or PHP (using the PHP Media API Wrapper).

For Drupal, developers can utilize the excellent Brightcove module to create Brightcove video fields that allow for video browsing and uploading as well as views integration. The Drupal Brightcove module utilizes the PHP API wrapper mentioned above.

Recently, a client requested the ability to upload certain field data from the nodes that contained Brightcove video fields to the Brightcove Video Cloud for the selected video. They upload their videos to Brightcove outside of Drupal, so they required the Upload button for the Brightcove module be disabled and required the addition of a new AJAX-enabled button to upload their data.

To add the Upload Metadata button to the Brightcove video field, hook_field_widget_form is implemented:

  1. function mybcmod_field_widget_form_alter(&$element, &$form_state, $context) {
  2.   //Place the button on the node edit form only
  3.   if (arg(0)  'node' && arg(2)  'edit') {
  4.     if ($context['field']['type'] == 'brightcove_field' && user_access('browse videos')) {
  5.  
  6.       // Add button to upload metadata to a video
  7.       // Based on Brightcove module code
  8.       $element['actions'][$action] = array(
  9.         '#type' => 'submit',
  10.         '#value' => t('Upload Metadata'),
  11.         '#name' => $element['#field_name'] . '-' . $context['delta'],
  12.         '#attributes' => array(
  13.           'class' => array('brightcove-field-upload-button'),
  14.         ),
  15.         // Do not use executes_submit_callback=false or limit_validation_errors
  16.         // These will prevent the button from working more than once without a page refresh
  17.         '#ajax' => array(
  18.          'callback' => 'mybcmod_ajax_display_metadata',
  19.         ),
  20.       );
  21.     }
  22.     return $element;
  23.   }
  24. }

Upload Metadata screenshot

For the AJAX callback above, a preview (using AJAX commands defined in the Brightcove module) dialog box is provided to allow the user to review the data being uploaded, and make changes as needed.

  1. function mybcmod_ajax_display_metadata($form, $form_state) {
  2.   $field_rel = $form_state['triggering_element']['#name'];
  3.  
  4.   $title = 'Brightcove Metadata Upload';
  5.   $id = 'upload-dialog';
  6.   $selector = '<div id="' . $id . '">';
  7.  
  8.   // This form displays the field data to be uploaded to Brightcove
  9.   $popup_form = drupal_get_form('mybcmod_uploadmetadata_form', $form_state['values']);
  10.   $rendered_form = drupal_render($popup_form);
  11.  
  12.   $commands = array();
  13.   // This command dialog is provided by the Brightcove module
  14.   $commands[] = ajax_command_dialog($title, $id, $selector, $rendered_form, $field_rel, null, false);
  15.  
  16.   return array('#type' => 'ajax', '#commands' => $commands);
  17. }

Metadata Preview screenshot

The form above retrieves the field data from the node as requested, specifically title, summary, body and any taxonomy terms. The form’s Attach Metadata button is defined with the :

  1. $form['upload'] = array(
  2.   '#type' => 'submit',
  3.   '#name' => 'attach',
  4.   '#default_value' => t('Attach Metadata'),
  5.   '#ajax' => array(
  6.     'callback' => 'mybcmod_ajax_upload_metadata',
  7.   ),
  8. );

The callback function in the form above is where the data actually gets transferred to Brightcove.

  1. function mybcmod_ajax_upload_metadata($form, $form_state) {
  2.  
  3.   $values = $form_state['values'];
  4.   $nid = check_plain($values['nid']);
  5.  
  6.   // populate the metadata array (fields of the Brightcove Video object)
  7.   $meta = array(
  8.     'id' => check_plain($values['video_id']),
  9.     'name' => filter_xss($values['title']),
  10.     'shortDescription' => filter_xss($values['short']),
  11.     'longDescription' => filter_xss($values['long']),
  12.     'tags' => explode(', ', check_plain($values['tags'])),
  13.     'linkText' => t('Read more'),
  14.     'linkURL' => url('node/' . $nid, array('absolute' => true))
  15.   );
  16.  
  17.   $success = true;
  18.  
  19.   try {
  20.     // convenience function provided by Brightcove module
  21.     // that returns a Brightcove Media api object from bc-mapi.php library
  22.     // uses settings supplied at /admin/config/media/brightcove
  23.     $bc = brightcove_initialize();
  24.  
  25.     // bmapi method that transfers the data to the Brightcove video cloud
  26.     $bc->update('video', $meta);
  27.   }
  28.   catch(Exception $ex) {
  29.     watchdog('BRIGHTCOVE', $ex->getMessage());
  30.     $success = false;
  31.   }
  32.  
  33.   if ($success) {
  34.     $commands = array();
  35.     // Close Dialog command provided by Brightcove module
  36.     $commands[] = ajax_command_close_dialog('#upload-dialog', '', null, 'close');
  37.  
  38.     return array('#type' => 'ajax', '#commands' => $commands);
  39.   }
  40.   else {
  41.     $commands = array();
  42.     $commands[] = ajax_command_alert("Error uploading metadata to Brightcove");
  43.     return array('#type' => 'ajax', '#commands' => $commands);
  44.   }
  45. }

 

Depending on your setup you can verify the correct data has been assigned to the proper video by viewing the video or by logging into the Brightcove Video cloud and reviewing the data.

If you need other custom functionality not provided by the Drupal Brightcove module, the Brightcove PHP Media API Wrapper is worth exploring as it provides functionality finding and sharing videos, creating playlists and much more.

Bill Renk