You are here

function media_youtube_preprocess_media_youtube_video in Media: YouTube 7

Same name and namespace in other branches
  1. 7.3 themes/media_youtube.theme.inc \media_youtube_preprocess_media_youtube_video()
  2. 7.2 themes/media_youtube.theme.inc \media_youtube_preprocess_media_youtube_video()

Preprocess function for theme('media_youtube_video').

File

includes/themes/media_youtube.theme.inc, line 12
media_youtube/includes/themes/media_youtube.theme.inc

Code

function media_youtube_preprocess_media_youtube_video(&$variables) {

  // Build the URL for display.
  $uri = $variables['uri'];
  $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
  $parts = $wrapper
    ->get_parameters();
  $variables['video_id'] = check_plain($parts['v']);
  $variables['query'] = array();

  // @see http://code.google.com/apis/youtube/player_parameters.html
  foreach (array(
    'width',
    'height',
    'autoplay',
    'related',
    'hd',
    'showsearch',
    'modestbranding',
    'showinfo',
    'version',
    'theme',
    'fullscreen',
    'wmode',
    'chromeless',
  ) as $option) {

    // Set the option, either from the options array, or from the default value.
    $variables[$option] = isset($variables[$option]) ? $variables[$option] : (isset($variables['options'][$option]) ? $variables['options'][$option] : media_youtube_variable_get($option));
  }

  // We have to set fullscreen in the url query and as a parameter to the flash.
  $variables['fs'] = $variables['fullscreen'];
  $variables['fullscreen'] = $variables['fullscreen'] ? 'true' : 'false';
  $variables['wrapper_id'] = 'media_youtube_' . $variables['video_id'] . '_' . $variables['id'];

  // Pass the settings to replace the object tag with an iframe.
  $settings = array(
    'media_youtube' => array(
      $variables['wrapper_id'] => array(
        'width' => $variables['width'],
        'height' => $variables['height'],
        'video_id' => $variables['video_id'],
        'fullscreen' => $variables['fullscreen'],
        'id' => $variables['wrapper_id'] . '_iframe',
      ),
    ),
  );

  // Set the version of the youtube api player.
  if ($variables['version']) {
    $variables['query']['version'] = $variables['version'];

    // Note that the fs variable defaults to 1 with the AS3 player.
    if (!$variables['fs']) {
      $variables['query']['fs'] = 0;
    }
  }
  else {
    if ($variables['fs']) {

      // Note that the fs variable defaults to 0 with the AS2 player.
      $variables['query']['fs'] = 1;
    }
  }

  // These options default to 0.
  foreach (array(
    'modestbranding',
    'autoplay',
    'hd',
  ) as $variable) {
    if ($variables[$variable]) {
      $variables['query'][$variable] = 1;
    }
  }

  // These options default to 1.
  foreach (array(
    'showsearch',
    'showinfo',
  ) as $variable) {
    if (!$variables[$variable]) {
      $variables['query'][$variable] = 0;
    }
  }
  if (!$variables['related']) {
    $variables['query']['rel'] = 0;
  }
  if ($variables['theme'] != 'dark') {
    $variables['query']['theme'] = $variables['theme'];
  }

  // Ensure that we pass the required query variables to the Iframe settings.
  if (!empty($variables['query'])) {
    $settings['media_youtube'][$variables['wrapper_id']]['options'] = $variables['query'];
  }
  drupal_add_js($settings, 'setting');
  drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/media_youtube.js');
  drupal_add_css(drupal_get_path('module', 'media_youtube') . '/css/media_youtube.css');
  drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/flash_detect_min.js');

  // The chromeless player requires a different url.
  if ($variables['chromeless']) {
    $variables['url_api'] = 'apiplayer';
    $variables['query']['video_id'] = $variables['video_id'];
  }
  else {
    $variables['url_api'] = 'v/' . $variables['video_id'];
  }
  $variables['url'] = url('http://www.youtube.com/' . $variables['url_api'], array(
    'query' => $variables['query'],
    'external' => TRUE,
    'https' => TRUE,
  ));

  // For users with JavaScript, these object and embed tags will be replaced
  // by an iframe, so that we can support users without Flash.
  $variables['output'] = <<<OUTPUT
<!--[if !IE]> -->
  <object type="application/x-shockwave-flash" data="{<span class="php-variable">$variables</span>[<span class="php-string">'url'</span>]}" width="{<span class="php-variable">$variables</span>[<span class="php-string">'width'</span>]}" height="{<span class="php-variable">$variables</span>[<span class="php-string">'height'</span>]}">
<!-- <![endif]-->
<!--[if IE]>
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="{<span class="php-variable">$variables</span>[<span class="php-string">'width'</span>]}" height="{<span class="php-variable">$variables</span>[<span class="php-string">'height'</span>]}">
    <param name="movie" value="{<span class="php-variable">$variables</span>[<span class="php-string">'url'</span>]}" />
      <param name="allowFullScreen" value="{<span class="php-variable">$variables</span>[<span class="php-string">'fullscreen'</span>]}"></param>
      <param name="wmode" value="{<span class="php-variable">$variables</span>[<span class="php-string">'wmode'</span>]}" />
<!-->
  </object>
<!-- <![endif]-->
OUTPUT;
}