[Drupal] How to embed a kaltura video programmatically in a popup in a Drupal 7 website?

| | 2 min read

If we are using kaltura field in a content type in a Drupal website, the video uploaded using this kaltura field will be displayed automatically in corresponding node page. One of our Drupal client had a requirement that, on clicking a button, load a popup window and play kaltura video in that popup. Read on to know how to embed a Kaltura video programmatically in a popup in a Drupal 7 website

  1. Create menu to load kaltura video and pass nid as argument
  2. In the page callback function add code to embed kaltura video.
    Steps to embed kaltura video are,
    • Load node using nid
    • Get all data like kaltura_entryid and mediatype of that kaltura field.
    • Get all details about video from 'node_kaltura' table using 'kaltura_entryid'.
    • Build embed code for that video using details retrieved from 'node_kaltura' table using function field_kaltura_build_embed.
    • Theme the kaltura player and then print output.
  3. Load this menu in popup window.

The programatic implementation of the above steps is given below:

  • Create menu to load kaltura video ,
    
    /**
     *Implement hook_menu()
     */
    function module_name_menu() {
      $menu['film/popup/trailer/%'] = array(
        'title' => 'popup',
        'description' => 'popup menu',
        'page callback' => 'module_name_trailer_video',
        'access callback' => TRUE,
      );
      return $menu;
    }
    
  • In the page callback function add code embed kaltura video,
    
    function module_name_trailer_video() {
      $nid = arg(3);
      
      // Load node using nid. 
      $node = node_load($nid);
      
      // Get all data like kaltura_entryid and mediatype of that kaltura field.
      $field = field_info_field('field_kaltura_field_name');
      $settings = $field['settings'];
      if (isset($node->field_kaltura_field_name['und'][0]['entryid'])) {
        $item = $node->field_kaltura_field_name['und'][0]['entryid'];
      }
      if (isset( $node->field_kaltura_field_name['und'][0]['mediatype'])) {
        $type = $node->field_kaltura_field_name['und'][0]['mediatype'];
      }
      
      // Get all details about video from 'node_kaltura' table using 'kaltura_entryid'. 
      $query = db_select('node_kaltura', 'k')
        ->fields('k')
        ->condition('kaltura_entryid', $item, '=')
        ->execute()
        ->fetchAssoc();
      $user_id = $node->uid;
      $metadata['views'] = $query['kaltura_views'];
      $metadata['plays'] = $query['kaltura_plays'];
      $metadata['votes'] = $query['kaltura_votes'];
      $metadata['rank'] = $query['kaltura_rank'];
      $metadata['total_rank'] = $query['kaltura_total_rank'];
      $seometa['description'] = $query['kaltura_description'];
      $seometa['title'] = $query['kaltura_title'];
      $type = !empty($type) ? $type : $query['kaltura_media_type'];
      $path = '';
      $thumb_url = $query['kaltura_thumbnail_url'];
      $meta = theme('kaltura_metadata', array('metadata' => $metadata));
    
      // build embed code for that video using details retrieved from  'node_kaltura' table.
      $embed = field_kaltura_build_embed($item, $type, $settings, $path, $user_id, $thumb_url, $seometa);
    
      // Theme the kaltura player and then print output.
      $video_trailer = theme('kaltura_player', array('embed' => $embed, 'title' => $query['kaltura_title'], 'desc' => $query['kaltura_description']));
      $output = $video_trailer;
      print $output;
    }