You are here

function template_preprocess_jw_player in JW Player 7.2

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

Process variables for jw_player.tpl.php.

@todo this whole function seems obsolete. The config passed to jw player should be prepared right where the theme function is called and passed down to js using js settings in #attached within the render array. Along width that, inline snippets in the template should be replaced by a a behavior that initializes the players with the options from js settings.

File

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

Code

function template_preprocess_jw_player(&$variables) {

  //In some instances an object is passed so convert to an array.
  if (is_object($variables['file'])) {
    $variables['file'] = (array) $variables['file'];
  }

  // 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'];
      if (!empty($preset_settings['responsive'])) {
        unset($preset_settings['height']);
        $preset_settings['width'] = $preset_settings['width'] . '%';
      }
      else {
        unset($preset_settings['aspectratio']);
      }
    }
  }

  // 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']['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 (isset($variables['sources'])) {
    $variables['config']['playlist'][0] = array();
    $variables['config']['playlist'][0]['image'] = file_create_url($variables['image']['uri']);
    foreach ($variables['sources'] as $key => $source) {
      $variables['config']['playlist'][0]['sources'][$key]['file'] = file_create_url($source['uri']);
      if (isset($source['getid3']) && $source['getid3']['width'] > 0 && $source['getid3']['height'] > 0) {
        $variables['config']['playlist'][0]['sources'][$key]['width'] = $source['getid3']['width'];
        $variables['config']['playlist'][0]['sources'][$key]['height'] = $source['getid3']['height'];
      }
      elseif (isset($source['field_width']) && isset($source['field_height'])) {
        $variables['config']['playlist'][0]['sources'][$key]['width'] = $source['field_width'][LANGUAGE_NONE][0]['value'];
        $variables['config']['playlist'][0]['sources'][$key]['height'] = $source['field_height'][LANGUAGE_NONE][0]['value'];
      }
    }
  }
  elseif (isset($variables['playlist'])) {
    $variables['config']['playlist'] = array();
    foreach ($variables['playlist'] as $key => $source) {
      $variables['config']['playlist'][$key]['file'] = file_create_url($source['uri']);
      $variables['config']['playlist'][$key]['image'] = 'none';
      if (!empty($source['description'])) {
        $variables['config']['playlist'][$key]['title'] = $source['description'];
      }
      else {
        $pathinfo = pathinfo($source['uri']);
        $variables['config']['playlist'][$key]['title'] = $pathinfo['filename'];
      }
    }
    $variables['config']['listbar'] = array(
      'position' => !empty($variables['playlist_position']) ? $variables['playlist_position'] : JW_PLAYER_DEFAULT_PLAYLIST_POSITION,
      'size' => !empty($variables['playlist_size']) ? $variables['playlist_size'] : JW_PLAYER_DEFAULT_PLAYLIST_POSITION,
    );
  }
  else {
    $variables['config']['file'] = file_create_url($variables['file']['uri']);
  }
  if (jw_player_use_legacy()) {

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

  // Outside of the cloud player check, has to work then as well.
  if (!empty($variables['image'])) {
    $variables['config']['image'] = $variables['image'];
  }

  // Don't apply preset config in case of cloud hosted players.
  if (!variable_get('jw_player_cloud_player_default', FALSE)) {

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

    // If the preset has the primary mode set, modify the modes array so that it
    // comes first.
    if (isset($variables['primary']) && $variables['primary'] == 'flash') {
      $variables['config']['primary'] = 'flash';
    }
  }
}