You are here

function youtube_field_formatter_view in YouTube Field 7

Implements hook_field_formatter_view().

File

./youtube.module, line 486

Code

function youtube_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {

    // This formatter outputs the youtube embed code.
    case 'youtube_video':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'youtube_video',
          '#input' => $item['input'],
          '#video_id' => $item['video_id'],
          '#entity_title' => !empty($entity->title) ? check_plain($entity->title) : NULL,
          '#size' => array_key_exists('youtube_size', $display['settings']) ? $display['settings']['youtube_size'] : NULL,
          '#width' => array_key_exists('youtube_width', $display['settings']) ? $display['settings']['youtube_width'] : NULL,
          '#height' => array_key_exists('youtube_height', $display['settings']) ? $display['settings']['youtube_height'] : NULL,
          '#autoplay' => array_key_exists('youtube_autoplay', $display['settings']) ? $display['settings']['youtube_autoplay'] : FALSE,
          '#mute' => array_key_exists('youtube_mute', $display['settings']) ? $display['settings']['youtube_mute'] : FALSE,
          '#loop' => array_key_exists('youtube_loop', $display['settings']) ? $display['settings']['youtube_loop'] : FALSE,
          '#controls' => array_key_exists('youtube_controls', $display['settings']) ? $display['settings']['youtube_controls'] : FALSE,
          '#autohide' => array_key_exists('youtube_autohide', $display['settings']) ? $display['settings']['youtube_autohide'] : FALSE,
          '#iv_load_policy' => array_key_exists('youtube_iv_load_policy', $display['settings']) ? $display['settings']['youtube_iv_load_policy'] : FALSE,
          '#playsinline' => array_key_exists('youtube_playsinline', $display['settings']) ? $display['settings']['youtube_playsinline'] : FALSE,
          '#allow_autoplay' => array_key_exists('youtube_allow_autoplay', $display['settings']) ? $display['settings']['youtube_allow_autoplay'] : FALSE,
          '#allow_fullscreen' => array_key_exists('youtube_allow_fullscreen', $display['settings']) ? $display['settings']['youtube_allow_fullscreen'] : FALSE,
        );
      }
      break;

    // This formatter uses an imagecache preset to generate a thumbnail.
    case 'youtube_thumbnail':

      // Check if the formatter involves a link.
      if (isset($display['settings']['image_link'])) {
        switch ($display['settings']['image_link']) {
          case 'content':
            $uri = entity_uri($entity_type, $entity);
            $uri['options']['html'] = TRUE;
            break;
          case 'youtube':
            $link_youtube = TRUE;
            break;
        }
      }
      foreach ($items as $delta => $item) {

        // If the thumbnail is linked to it's YouTube page, take the original
        // URL.
        if (!empty($link_youtube)) {
          $uri = array(
            'path' => $item['input'],
            'options' => array(
              'html' => TRUE,
            ),
          );
        }

        // Allow other modules that have added link types to build the uri that
        // the thumbnail links to when using their link type.
        drupal_alter('youtube_thumbnail_link_uri', $uri, $display['settings'], $item);
        $element[$delta] = array(
          '#theme' => 'youtube_thumbnail',
          '#video_id' => $item['video_id'],
          '#entity_title' => !empty($entity->title) ? check_plain($entity->title) : NULL,
          '#image_style' => $display['settings']['image_style'],
          '#image_link' => isset($uri) ? $uri : '',
        );
      }
      break;

    // This formatter outputs the YouTube URL as either a link or plain text.
    case 'youtube_url':
      foreach ($items as $delta => $item) {
        if (!empty($display['settings']['link'])) {
          $element[$delta] = array(
            '#theme' => 'link',
            '#text' => check_plain($item['input']),
            '#path' => $item['input'],
            '#options' => array(
              'attributes' => array(
                'class' => array(
                  'youtube-url',
                  'youtube-url--' . drupal_html_class($item['video_id']),
                ),
              ),
              'html' => TRUE,
            ),
          );
        }
        else {
          $element[$delta] = array(
            '#markup' => check_plain($item['input']),
          );
        }
      }
      break;
  }
  return $element;
}