You are here

function template_preprocess_youtube_video in YouTube Field 8

Prepares variables for the YouTube Video template.

Default template: youtube-video.html.twig.

File

./youtube.module, line 100
Youtube field module adds a field for YouTube videos.

Code

function template_preprocess_youtube_video(&$variables) {
  $config = \Drupal::config('youtube.settings');

  // Add global YouTube module configuration to the settings array.
  $variables['settings'] += [
    'youtube_suggest' => $config
      ->get('youtube_suggest'),
    'youtube_privacy' => $config
      ->get('youtube_privacy'),
    'youtube_player_class' => $config
      ->get('youtube_player_class'),
    'youtube_modestbranding' => $config
      ->get('youtube_modestbranding'),
    'youtube_theme' => $config
      ->get('youtube_theme'),
    'youtube_color' => $config
      ->get('youtube_color'),
    'youtube_enablejsapi' => $config
      ->get('youtube_enablejsapi'),
    'youtube_wmode' => $config
      ->get('youtube_wmode'),
  ];

  // Build the query for the embedded video using module and field settings.
  $query = [];
  if (!$variables['settings']['youtube_suggest']) {
    $query['rel'] = '0';
  }
  if ($variables['settings']['youtube_modestbranding']) {
    $query['modestbranding'] = '1';
  }
  if ($variables['settings']['youtube_theme']) {
    $query['theme'] = 'light';
  }
  if ($variables['settings']['youtube_color']) {
    $query['color'] = 'white';
  }
  if ($variables['settings']['youtube_enablejsapi']) {
    global $base_root;
    $query['enablejsapi'] = '1';
    $query['origin'] = $base_root;
  }
  if ($variables['settings']['youtube_wmode']) {
    $query['wmode'] = 'opaque';
  }
  if ($variables['settings']['youtube_autoplay']) {
    $query['autoplay'] = '1';
  }
  if ($variables['settings']['youtube_mute']) {
    $query['mute'] = '1';
  }
  if ($variables['settings']['youtube_loop']) {
    $query['loop'] = '1';
    $query['playlist'] = $variables['video_id'];
  }
  if ($variables['settings']['youtube_controls']) {
    $query['controls'] = '0';
  }
  if ($variables['settings']['youtube_autohide']) {
    $query['autohide'] = '1';
  }
  if ($variables['settings']['youtube_iv_load_policy']) {
    $query['iv_load_policy'] = '3';
  }

  // If the override setting is enabled, add any additional parameters provided
  // in the initial field value to the query of the embedded video.
  if ($config
    ->get('youtube_override')) {
    if ($url_parts = UrlHelper::parse($variables['input'])) {
      foreach ($url_parts['query'] as $key => $value) {
        if ($key == 'v') {
          continue;
        }
        $query[$key] = $value;
      }
    }
  }

  // Use the module's privacy configuration to determine the domain.
  $domain = !$variables['settings']['youtube_privacy'] ? 'youtube.com' : 'youtube-nocookie.com';
  $path = 'https://www.' . $domain . '/embed/' . $variables['video_id'];

  // Build the src attribute with the path and query array constructed above.
  $url = Url::fromUri($path, [
    'query' => $query,
  ]);
  $variables['content_attributes']['src'] = $url
    ->toString();

  // Use the field's display settings to retrieve the video's dimensions.
  $size = $variables['settings']['youtube_size'];
  $width = $variables['settings']['youtube_width'];
  $height = $variables['settings']['youtube_height'];
  if ($size != 'responsive') {
    $dimensions = youtube_get_dimensions($size, $width, $height);

    // Assign the retrieved dimensions as attributes on the iframe element.
    $variables['content_attributes']['width'] = $dimensions['width'];
    $variables['content_attributes']['height'] = $dimensions['height'];
  }

  // Build the iframe element's id and class attribute values.
  $class = $variables['settings']['youtube_player_class'];
  $id = Html::getUniqueId($class);
  $variables['content_attributes']['id'] = $id;
  $variables['content_attributes']['class'][] = Html::getClass($class);

  // Build the iframe element's title attribute value (for accessibility).
  $variables['content_attributes']['title'] = t('Embedded video');
  if (!empty($variables['entity_title'])) {
    $variables['content_attributes']['title'] = t('Embedded video for @entity_title', [
      '@entity_title' => $variables['entity_title'],
    ]);
  }

  // Alternative content for browsers that don't understand iframes (WCAG).
  $variables['content_attributes']['aria-label'] = $variables['content_attributes']['title'] . ': ' . $url
    ->toString();

  // Remove iframe border.
  $variables['content_attributes']['frameborder'] = 0;

  // Add classes to the wrapping element.
  $variables['attributes']['class'][] = 'youtube-container';
  if ($size == 'responsive') {

    // When the "responsive" size is chosen in the field's display settings,
    // this class is used by the module's CSS to make the player responsive.
    $variables['attributes']['class'][] = 'youtube-container--responsive';
  }
}