You are here

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

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.
 *
 * @see template_process()
 */
function videojs_preprocess_videojs(&$vars) {
  videojs_add();
  $items = $vars['items'];
  $codecs = array(
    'video/mp4' => 'video/mp4',
    'video/x-m4v' => 'video/mp4',
    'video/webm' => 'video/webm',
    'application/octet-stream' => 'video/webm',
    'video/ogg' => 'video/ogg',
    'application/ogg' => 'video/ogg',
    'video/quicktime' => 'video/mp4',
    'video/flv' => 'video/flv',
    'video/x-flv' => 'video/flv',
    'audio/mpeg' => 'audio/mpeg',
    'audio/ogg' => 'audio/ogg',
    'application/vnd.apple.mpegurl' => 'application/vnd.apple.mpegurl',
  );
  $settings = videojs_utility::getDisplaySettings($vars['attributes']);
  $vars['attributes_array']['data-setup'] = '{}';

  // Set the element ID
  $vars['attributes_array']['id'] = $vars['player_id'] . '-video';

  // Set the video's width and height
  $vars['attributes_array']['width'] = $settings['width'];
  $vars['attributes_array']['height'] = $settings['height'];

  // Set preload.
  $vars['attributes_array']['preload'] = $settings['preload'];

  // Display or hide the controls.
  if (!$settings['hidecontrols']) {
    $vars['attributes_array']['controls'] = 'controls';
  }

  // Display or hide the controls.
  if ($settings['loop']) {
    $vars['attributes_array']['loop'] = 'loop';
  }

  // Allow autoplay
  if ($settings['autoplay']) {
    $vars['attributes_array']['autoplay'] = 'autoplay';
  }
  $vars['items'] = array();
  $vars['tracks'] = array();
  foreach ($items as $item) {

    // Add src variables.
    if (!empty($item['uri'])) {
      $src = file_create_url($item['uri']);
      $item['src'] = array(
        'raw' => $src,
        'safe' => check_plain($src),
      );
    }
    if (empty($item['filemime'])) {
      continue;
    }
    if (!isset($codecs[$item['filemime']])) {

      // check for image file and add in it as poster
      if (strncmp($item['filemime'], 'image/', 6) === 0) {
        $settings['poster'] = $item['uri'];
      }

      // Filter out tracks.
      if (strncmp($item['filemime'], 'text/vtt', 8) === 0) {

        // Add raw/safe for videotype var.
        $item['filemime'] = array(
          'raw' => $item['filemime'],
          'safe' => check_plain($item['filemime']),
        );
        $vars['tracks'][] = $item + array(
          'default' => FALSE,
          'kind' => 'subtitles',
          'charset' => 'utf-8',
          'label' => '',
          'langcode' => '',
        );
      }

      // Skip unplayable items.
      continue;
    }

    // Add raw/safe for videotype var.
    $item['videotype'] = array(
      'raw' => $codecs[$item['filemime']],
      'safe' => check_plain($codecs[$item['filemime']]),
    );
    $vars['items'][] = $item;
  }

  // Turn the poster image URI into a URL, if it isn't an URL already.
  if (!empty($settings['poster']) && strncmp($settings['poster'], 'http://', 7) !== 0 && strncmp($settings['poster'], 'https://', 8) !== 0) {
    if (empty($vars['posterimage_style'])) {
      $vars['attributes_array']['poster'] = file_create_url($settings['poster']);
    }
    else {
      $vars['attributes_array']['poster'] = image_style_url($vars['posterimage_style'], $settings['poster']);
    }
  }

  // Default classes.
  $vars['classes_array'] = array(
    'video-js',
    'vjs-default-skin',
  );

  // Add class to center the vjs big play button, if configured to do so.
  if (!empty($settings['centeredplaybutton'])) {
    $vars['classes_array'][] = 'vjs-big-play-centered';
  }
}

/**
 * Preprocess function for videojs-media-wysiwyg-preview.tpl.php.
 */
function videojs_preprocess_videojs_media_wysiwyg_preview(&$vars) {
  videojs_preprocess_videojs($vars);

  // If the video has no poster image, borrow one from the media module.
  // This theme function should only be used by the media module, so assume
  // it is loaded.
  if (empty($vars['poster'])) {
    $poster = drupal_get_path('module', 'media') . '/images/icons/default/video-x-generic.png';
    $raw = file_create_url($poster);
    $vars['poster'] = array(
      'raw' => $raw,
      'safe' => check_plain($raw),
    );
  }
}