You are here

videojs.theme.inc in Video.js (HTML5 Video Player) 6.2

Theme and preprocess functions for the Video.js module.

File

includes/videojs.theme.inc
View source
<?php

/**
 * @file
 * Theme and preprocess functions for the Video.js module.
 */

/**
 * Preprocess function for videojs.tpl.php when using a playlist.
 */
function videojs_preprocess_videojs_formatter_videojs(&$vars) {
  $field_name = $vars['element']['#field_name'];
  $node = $vars['element']['#node'];
  $vars['player_id'] = $node->nid . '-' . str_replace('_', '-', $field_name);
  $vars['items'] = (array) $node->{$field_name};
  $vars['attributes'] = array(
    'width' => intval(variable_get('videojs_width', 640)),
    'height' => intval(variable_get('videojs_height', 264)),
  );

  // Find the poster images
  $poster_field = variable_get('videojs_' . $node->type, 'video_module');
  if ($poster_field == 'video_module') {
    if (!empty($vars['items'][0]['data']['video_thumb'])) {

      // Video module present, retrieving description and poster image from first video
      $vars['items']['thumbnail'] = $vars['items'][0]['data']['video_thumb'];
    }
  }
  else {

    // Grab any poster image from a field defined in videojs admin settings
    if (!empty($node->{$poster_field})) {

      // Take the first image from field
      $vars['items']['thumbnail'] = $node->{$poster_field}[0];
    }
  }

  // Preprocess link fields
  $field = content_fields($field_name, $node->type);
  if ($field['type'] == 'link') {
    $links = $vars['items'];
    $vars['items'] = array();
    foreach ($links as $link) {
      if (!empty($link['title']) && strncmp('video/', $link['title'], 6) === 0) {
        $link['filemime'] = $link['title'];
      }
      elseif (substr($link['url'], -5) == '.webm') {
        $link['filemime'] = 'video/webm';
      }
      else {
        $link['filemime'] = file_get_mimetype($link['url']);

        // If the mime type is application/octet-stream, default to MP4.
        // This happens for instance for links without extensions.
        if ($link['filemime'] == 'application/octet-stream') {
          $link['filemime'] = 'video/mp4';
        }
      }
      $link['url'] = url($link['url'], $link);
      if (strncmp($link['filemime'], 'image/', 6) == 0) {
        $vars['items']['thumbnail'] = $link;
      }
      else {
        $vars['items'][] = $link;
      }
    }
  }
  template_preprocess_videojs($vars);
}

/**
 * Preprocess function for videojs.tpl.php when displaying a view as a playlist.
 */
function template_preprocess_videojs_view(&$vars) {
  videojs_add();
  $vars['player_id'] = 'videojs-view-' . str_replace('_', '-', $vars['view']->name);
}

/**
 * Preprocess function for videojs.tpl.php when displaying a view as a playlist.
 */
function template_preprocess_videojs(&$vars) {
  videojs_add();
  $items = $vars['items'];
  $vars['player_id'] = 'videojs-' . str_replace('_', '-', $vars['player_id']);
  $vars['width'] = intval($vars['attributes']['width']);
  $vars['height'] = intval($vars['attributes']['height']);
  $vars['autoplay'] = !!variable_get('videojs_autoplay', FALSE);
  $vars['items'] = array();
  foreach ($items as $item) {
    $item['videotype'] = _videojs_get_filetype($item['filemime']);
    if ($item['videotype'] == NULL) {
      continue;
    }
    $item['url'] = _videojs_create_url($item);
    $vars['items'][] = $item;
  }
  $vars['poster'] = NULL;
  if (!empty($items['thumbnail'])) {
    $vars['poster'] = _videojs_create_url($items['thumbnail']);
  }
}
function _videojs_create_url($file) {
  if (!empty($file['url'])) {
    return $file['url'];
  }
  if (!empty($file['filepath'])) {
    if (strncmp($file['filepath'], 'http:', 5) === 0 || strncmp($file['filepath'], 'https:', 6) === 0) {
      return $file['filepath'];
    }
    return file_create_url(drupal_urlencode($file['filepath']));
  }
  return '';
}
function _videojs_get_filetype($filemime) {
  switch (drupal_strtolower($filemime)) {
    case 'video/mp4':
    case 'video/quicktime':
      return 'video/mp4';
    case 'video/ogg':
    case 'application/ogg':
      return 'video/ogg';
    case 'video/webm':
    case 'application/octet-stream':
      return 'video/webm';
    case 'video/flv':
    case 'video/x-flv':
      return 'video/flv';
    case 'audio/mpeg':
      return 'audio/mpeg';
    case 'audio/ogg':
      return 'audio/ogg';
    case 'application/vnd.apple.mpegurl':
      return 'application/vnd.apple.mpegurl';
  }
  return NULL;
}

Functions

Namesort descending Description
template_preprocess_videojs Preprocess function for videojs.tpl.php when displaying a view as a playlist.
template_preprocess_videojs_view Preprocess function for videojs.tpl.php when displaying a view as a playlist.
videojs_preprocess_videojs_formatter_videojs Preprocess function for videojs.tpl.php when using a playlist.
_videojs_create_url
_videojs_get_filetype