You are here

function scald_youtube_scald_prerender in Scald YouTube 7

Implements hook_scald_prerender().

File

./scald_youtube.module, line 279
Defines a YouTube provider for Scald.

Code

function scald_youtube_scald_prerender($atom, $context, $options, $mode) {
  if ($mode == 'atom') {

    // Load context configuration to retrieve dimension data if present.
    $context_config = scald_context_config_load($context);
    $video_width = !empty($atom->data['video_width']) ? $atom->data['video_width'] : 480;
    $video_height = !empty($atom->data['video_height']) ? $atom->data['video_height'] : 365;

    // Allow context configuration to override video dimension variables.
    if (!empty($context_config->data['width'])) {
      $video_width = $context_config->data['width'];
    }
    if (!empty($context_config->data['height'])) {
      $video_height = $context_config->data['height'];
    }
    $query = array();
    if (!empty($atom->data['list'])) {
      $query['list'] = $atom->data['list'];
    }
    if (isset($atom->data['show_related']) && empty($atom->data['show_related'])) {
      $query['rel'] = '0';
    }

    // Don't apply the autoplay on admin page.
    if (isset($atom->data['youtube_autoplay']) && !empty($atom->data['youtube_autoplay']) && !path_is_admin(current_path())) {
      $query['autoplay'] = '1';
    }
    if (isset($atom->data['youtube_mute']) && !empty($atom->data['youtube_mute'])) {
      $query['mute'] = '1';
    }
    $youtube_domain = SCALD_YOUTUBE_EMBED;
    $attached_files = array(
      'css' => array(
        drupal_get_path('module', 'scald_youtube') . '/css/scald_youtube_iframe.css',
      ),
    );
    $defer_enable = variable_get('scald_youtube_defer_videos', FALSE);
    $consent_enable = variable_get('scald_youtube_consent_enable', FALSE);
    $consent_text = variable_get('scald_youtube_consent_text', '');
    $consent_button = variable_get('scald_youtube_consent_button', 'Accept');
    if ($consent_enable) {
      $defer_enable = TRUE;
    }
    $delayed_cookie = variable_get('scald_youtube_delayed_cookie', FALSE);
    if ($delayed_cookie) {
      $youtube_domain = SCALD_YOUTUBE_NOCOOKIE_EMBED;
    }
    if ($defer_enable) {
      $attached_files['js'] = array(
        drupal_get_path('module', 'scald_youtube') . '/js/scald_youtube_defer_library.js',
        drupal_get_path('module', 'scald_youtube') . '/js/scald_youtube_defer.js',
      );
    }
    $video_url = url($youtube_domain . $atom->base_id, array(
      'query' => $query,
    ));
    $atom->rendered->player = array(
      '#theme' => 'scald_youtube_player',
      '#vars' => array(
        'video_id' => $atom->base_id,
        'video_width' => $video_width,
        'video_height' => $video_height,
        'video_url' => $video_url,
        'thumbnail' => $atom->rendered->thumbnail_source_url,
        'title' => check_plain($atom->title),
        'enable_defer' => $defer_enable,
        'enable_consent' => $consent_enable,
        'consent_text' => t($consent_text),
        'consent_button' => !empty($consent_button) ? t($consent_button) : FALSE,
      ),
      '#atom' => $atom,
      '#attached' => $attached_files,
    );
  }
}