You are here

function template_preprocess_jw_player in JW Player 7

Same name and namespace in other branches
  1. 7.2 jw_player.module \template_preprocess_jw_player()

Process variables for jw_player.tpl.php.

Parameters

$variables: An array that must contain one (and only one) of the following:

  • $file_object The file object you would like JW Player to play.
  • $sources If you would like to play a URL directly instead of a file object the URL can be passed directly to the theme function. The file URL must be provided within an array. This is helpful for playing back the contents of link fields. Optionally the array can contain multiple URLS to allow for multiple video formats or adaptive bitrate switching. Here is a sample sources array that contains two formats of a video:
$variables['sources'] = array(
  array(
    'file_path' => 'http://example.com/video.ogg',
    'file_mime' => 'video/ogg',
  ),
  array(
    'file_path' => 'http://example.com/video.m4v',
    'file_mime' => 'video/mp4',
  ),
);

To use adaptive bitrate switching two additional parameters - with and and bitrate are to be provided. *Note that adaptive bitrate is only supported in the flash version of the player. In preprocessing the player will automatically switch to flash playback mode when two sources are supplied with bitrates.

The following optional variables can also be set:

  • $preset (highly recommended!) The machine name of the JW Player preset you would like to use. Template preprocessing will take care of applying the preset's settings for you.
  • $image URL for the image ("poster" in HTML 5 video) to use as the video preview.
  • $options Additional options for the player. These options will override The module's defaults as well as the presets defaults.

See also

jw_player.tpl.php

File

./jw_player.module, line 313
Adds a theme function which allows theme developers to use the JW Player.

Code

function template_preprocess_jw_player(&$variables) {

  // If a file object has been passed populate the sources array with the
  // variables derived from it.
  if (isset($variables['file_object'])) {
    $variables['sources'] = array(
      array(
        'file_path' => file_create_url($variables['file_object']->uri),
        'file_mime' => $variables['file_object']->filemime,
      ),
    );
  }

  // Load defaults as the starting point.
  $default_settings = jw_player_default_settings();

  // Load preset if set.
  $preset_settings = array();
  if (!empty($variables['preset'])) {
    $preset = jw_player_preset_load($variables['preset']);

    // Additional check to ensure that the preset has actually loaded. This
    // prevents problems where a preset has been deleted but a field is still
    // configured to use it.
    if (!empty($preset)) {
      $preset_settings = $preset['settings'];
    }
  }

  // Get any preset override options that were sent through the formatter or
  // theme call.
  $options = array();
  if (isset($variables['options'])) {
    $options = $variables['options'];
    unset($variables['options']);
  }

  // Merge all variables together. Preset settings take priority over defaults,
  // variables passed directly to the theme function take priority over both.
  $variables = array_merge($default_settings, $preset_settings, $options, $variables);

  // The html_id should have been previously set. But we need to make
  // sure we do have one.
  if (!isset($variables['html_id'])) {
    $id = $variables['file_object']->fid . $variables['preset'];
    $variables['html_id'] = drupal_html_id('jwplayer' . $id);
  }

  // Check if there is one or multiple files. If one file then we set 'file', if
  // there are multiple files we set 'levels'. Note that levels is used for both
  // multiple video formats as well as for adaptive bitrates.
  if (count($variables['sources']) > 1) {
    $variables['config']['levels'] = array();
    foreach ($variables['sources'] as $key => $source) {
      $variables['config']['levels'][$key]['file'] = $source['file_path'];
      if (isset($source['bitrate'])) {
        $variables['config']['levels'][$key]['bitrate'] = $source['bitrate'];
      }
      if (isset($source['width'])) {
        $variables['config']['levels'][$key]['width'] = $source['width'];
      }
    }
  }
  else {
    $variables['config']['file'] = $variables['sources'][0]['file_path'];
  }

  // Resolve skin url
  $skin = !empty($variables['skin']) ? jw_player_skins($variables['skin']) : '';
  $variables['skin'] = !empty($skin) ? file_create_url($skin->uri) : '';

  // Copy player variables into their own array to be set as JavaScript
  // configuration.
  // @todo Bad smell here. Refactoring needed.
  $config_variables = array(
    'width',
    'height',
    'image',
    'controlbar',
    'playlist.position',
    'playlist.size',
    'skin',
    'autoplay',
  );
  foreach ($config_variables as $key) {
    if (!empty($variables[$key])) {
      $variables['config'][$key] = $variables[$key];
    }
  }

  // Initalize the player modes. The order of this array determines which
  // playback mode will be tried first before the browser falls back to the next
  // option. The default is html5 first, but this can be overridden by a preset
  // (see the code directly below).
  $variables['config']['modes'] = array(
    array(
      'type' => 'html5',
    ),
    array(
      'type' => 'flash',
      'src' => file_create_url(libraries_get_path('jwplayer') . '/player.swf'),
    ),
  );

  // If the preset has the primary mode set, modify the modes array so that it
  // comes first.
  if (isset($variables['mode'])) {
    foreach ($variables['config']['modes'] as $key => $value) {
      if ($value['type'] == $variables['mode']) {
        unset($variables['config']['modes'][$key]);
        array_unshift($variables['config']['modes'], $value);
      }
    }
  }

  // Copy over all enabled plugins into the 'config' section as this is the key
  // that is sent over to the player.
  if (!empty($variables['plugins'])) {
    foreach ($variables['plugins'] as $plugin => $info) {
      if (!$info['enable']) {
        continue;
      }
      $variables['config']['plugins'][$plugin] = $info;
    }
  }
}