You are here

public function ImageShadowboxFormatter::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

image_shadowbox/lib/Drupal/image_shadowbox/Plugin/Field/FieldFormatter/ImageShadowboxFormatter.php, line 150
Contains \Drupal\image_shadowbox\Plugin\field\formatter\ImageShadowboxFormatter.

Class

ImageShadowboxFormatter
Plugin implementation of the 'image_shadowbox' formatter.

Namespace

Drupal\image_shadowbox\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items) {
  $elements = array();
  $config = \Drupal::config('shadowbox.settings');
  $entity = $items
    ->getEntity();
  $shadowbox_enabled_path = _shadowbox_activation() && $config
    ->get('shadowbox_enabled');
  switch ($this
    ->getSetting('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_id != '' ? "shadowbox[{$gallery_id}]" : 'shadowbox';
  $class = $gallery_id != '' ? "sb-image sb-gallery sb-gallery-{$gallery_id}" : 'sb-image sb-individual';
  $compact = $this
    ->getSetting('compact');
  foreach ($items as $delta => $item) {
    if ($item->entity) {
      switch ($this
        ->getSetting('title')) {
        case 'alt':
          $title = $item->alt;
          break;
        case 'title':
          $title = $item->title;
          break;
        case 'node':
          $title = $items
            ->getEntity()
            ->label();
          break;
        default:
          $title = '';
          break;
      }
      $linked_style = $this
        ->getSetting('image_link');
      if ($linked_style) {
        $style = entity_load('image_style', $linked_style);
        $uri = $style
          ->buildUrl($item->entity
          ->getFileUri());
      }
      else {
        $uri = $item->entity
          ->getFileUri();
      }
      $shadowbox_thumbnail = array(
        '#theme' => 'shadowbox_thumbnail',
        '#path' => $item->entity
          ->getFileUri(),
        '#alt' => $item->alt,
        '#title' => $title,
        '#image_style' => $this
          ->getSetting('image_style'),
      );
      $elements[$delta] = array(
        '#theme' => 'shadowbox_formatter',
        '#innerHTML' => $delta == 0 || !$compact ? $shadowbox_thumbnail : '',
        '#title' => $title,
        '#url' => file_create_url($uri),
        '#rel' => $rel,
        '#class' => $class,
      );
      if ($shadowbox_enabled_path) {
        $elements[$delta]['#attached']['library'][] = 'shadowbox/shadowbox';
      }
    }
  }
  return $elements;
}