You are here

public function FileShadowboxFormatter::viewElements in Shadowbox 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

file_shadowbox/lib/Drupal/file_shadowbox/Plugin/Field/FieldFormatter/FileShadowboxFormatter.php, line 190
Contains \Drupal\file_shadowbox\Plugin\field\formatter\FileShadowboxFormatter.

Class

FileShadowboxFormatter
Plugin implementation of the 'file_shadowbox' formatter.

Namespace

Drupal\file_shadowbox\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items) {
  $elements = array();
  $entity = $items
    ->getEntity();
  $settings = $this
    ->getSettings();
  $config = \Drupal::config('shadowbox.settings');
  $shadowbox_enabled_path = _shadowbox_activation() && $config
    ->get('shadowbox_enabled');
  switch ($settings['gallery']) {
    case 'page':
      $gallery_id = 'gallery';
      break;
    case 'field':
      $gallery_id = $items
        ->getName();
      break;
    case 'nid':
      $gallery_id = implode('-', array(
        $entity
          ->getEntityTypeId(),
        $entity
          ->id(),
      ));
      break;
    case 'field_nid':
      $gallery_id = implode('-', array(
        $entity
          ->getEntityTypeId(),
        $entity
          ->id(),
        $items
          ->getName(),
      ));
      break;
    default:
      $gallery_id = '';
      break;
  }
  $rel_gallery = $gallery_id != '' ? "shadowbox[{$gallery_id}]" : 'shadowbox';
  $width = $settings['video_width'];
  $height = $settings['video_height'];
  $compact = $settings['compact'];
  foreach ($items as $delta => $item) {
    if ($item
      ->isDisplayed() && $item->entity) {
      switch ($settings['title']) {
        case 'node':
          $title = $items
            ->getEntity()
            ->label();
          break;
        case 'description':
          $title = $item->description;
          break;
        default:
          $title = '';
          break;
      }
      $autoplay = $config
        ->get('shadowbox_autoplay_movies');
      switch ($item->entity
        ->getMimeType()) {
        case 'video/youtube':
          $uri = $item->entity
            ->getFileUri();
          $youtube_id = substr($uri, strrpos($uri, '/') + 1);
          $url = 'http://www.youtube.com/embed/' . $youtube_id;
          $querystring = array();
          if ($autoplay) {
            $querystring['autoplay'] = 1;
          }
          $youtube_quality = $config
            ->get('shadowbox_youtube_quality');
          if ($youtube_quality != 'auto') {
            $querystring['vq'] = $youtube_quality;
          }
          $url = !empty($querystring) ? $url . '?' . http_build_query($querystring) : $url;
          $ico = 'youtube.png';

          // if (\Drupal::moduleHandler()->moduleExists('media_youtube')) {
          //   $path = file_stream_wrapper_get_instance_by_uri($uri)->getLocalThumbnailPath();
          //   $image_style = $display['settings']['image_style'];
          // }
          // else {
          $path = 'http://img.youtube.com/vi/' . $youtube_id . '/0.jpg';
          $image_style = '';

          // }
          $attributes = array(
            'width' => $settings['video_thumb'] . 'px',
          );
          $rel = $rel_gallery . '; width=' . $width . '; height=' . $height;
          break;
        case 'video/vimeo':

          // if (\Drupal::moduleHandler()->moduleExists('media_vimeo')) {
          //   $uri = $item->entity->getFileUri();
          //   $parts = file_stream_wrapper_get_instance_by_uri($uri)->get_parameters();
          //   $vimeo_id = intval($parts['v']);
          //   $url = 'http://player.vimeo.com/video/' . $vimeo_id;
          //   $path = file_stream_wrapper_get_instance_by_uri($uri)->getLocalThumbnailPath();
          //   $image_style = $settings['image_style'];
          // }
          // else {
          $url = 'http://player.vimeo.com/video/' . $item['filename'];
          $path = _get_vimeo_thumbnail($item->entity
            ->getFilename());
          $image_style = '';

          // }
          $url = $autoplay ? $url . '?autoplay=1' : $url;
          $ico = 'vimeo.png';
          $attributes = array(
            'width' => $settings['video_thumb'] . 'px',
          );
          $rel = $rel_gallery . '; width=' . $width . '; height=' . $height;
          break;
        case 'video/quicktime':
          $url = file_create_url($item->entity
            ->getFileUri());
          $ico = 'mov.png';
          $path = FILE_SHADOWBOX_ICOPATH . $ico;
          $image_style = '';
          $rel = $rel_gallery . '; width=' . $width . '; height=' . $height;
          break;
        case 'video/x-ms-wmv':
          $url = file_create_url($item->entity
            ->getFileUri());
          $ico = 'wmv.png';
          $path = FILE_SHADOWBOX_ICOPATH . $ico;
          $image_style = '';
          $rel = $rel_gallery . '; width=' . $width . '; height=' . $height;
          break;
        case 'video/x-flv':
          $url = file_create_url($item->entity
            ->getFileUri());
          $ico = 'flv.png';
          $path = FILE_SHADOWBOX_ICOPATH . $ico;
          $image_style = '';
          $rel = $rel_gallery . '; width=' . $width . '; height=' . $height;
          break;
        case 'application/x-shockwave-flash':
          $url = file_create_url($item->entity
            ->getFileUri());
          $ico = 'swf.png';
          $path = FILE_SHADOWBOX_ICOPATH . $ico;
          $image_style = '';
          $rel = $rel_gallery . '; player=swf; width=' . $width . '; height=' . $height;
          break;
        case 'application/pdf':
          $url = file_create_url($item->entity
            ->getFileUri());
          $ico = 'pdf.png';
          $path = FILE_SHADOWBOX_ICOPATH . $ico;
          $image_style = '';
          $rel = '';
          break;
        default:
          if (strstr($item->entity
            ->getMimeType(), 'image/')) {
            $linked_style = $settings['image_link'];
            if ($linked_style) {
              $style = entity_load('image_style', $linked_style);
              $uri = $style
                ->buildUrl($item->entity
                ->getFileUri());
              if (!file_exists($uri)) {
                $uri = $style
                  ->buildUrl($item->entity
                  ->getFileUri());
              }
            }
            else {
              $uri = $item->entity
                ->getFileUri();
            }
            $url = file_create_url($uri);
            $ico = 'image.png';
            $path = $item->entity
              ->getFileUri();
            $image_style = $settings['image_style'];
            $rel = $rel_gallery;
          }
          else {
            $url = file_create_url($item->entity
              ->getFileUri());
            $ico = 'generic.png';
            $path = FILE_SHADOWBOX_ICOPATH . $ico;
            $image_style = '';
            $rel = '';
          }
          break;
      }
      $shadowbox_thumbnail = array(
        '#theme' => 'shadowbox_thumbnail',
        '#path' => $settings['icon'] === 'ico' ? FILE_SHADOWBOX_ICOPATH . $ico : $path,
        '#title' => $title,
        '#alt' => $title,
        '#attributes' => isset($attributes) ? $attributes : NULL,
      );
      $elements[$delta] = array(
        '#theme' => 'shadowbox_formatter',
        '#innerHTML' => $delta == 0 || !$compact ? $shadowbox_thumbnail : '',
        '#title' => $title,
        '#url' => $url,
        '#rel' => $rel,
        '#class' => $gallery_id != '' ? "sb-image sb-gallery sb-gallery-{$gallery_id}" : 'sb-image sb-individual',
      );
      if ($shadowbox_enabled_path) {
        $elements[$delta]['#attached']['library'][] = 'shadowbox/shadowbox';
      }
    }
  }
  return $elements;
}