You are here

public function ImageLightBoxFormatter::viewElements in ImageLightbox 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/ImagelightboxFormatter.php \Drupal\imagelightbox\Plugin\Field\FieldFormatter\ImageLightBoxFormatter::viewElements()
  2. 2.0.x src/Plugin/Field/FieldFormatter/ImagelightboxFormatter.php \Drupal\imagelightbox\Plugin\Field\FieldFormatter\ImageLightBoxFormatter::viewElements()

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 ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/ImagelightboxFormatter.php, line 105

Class

ImageLightBoxFormatter
Plugin implementation of the 'imagelightbox' formatter.

Namespace

Drupal\imagelightbox\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  $settings = $this
    ->getSettings();
  $cache_tags = [];
  if ($settings['image_style']) {
    $image_style = $this->imageStyleStorage
      ->load($settings['image_style']);
    $cache_tags = $image_style
      ->getCacheTags();
  }

  // Prepare image styles.
  if ($settings['imagelightbox_image_style']) {

    /** @var \Drupal\image\ImageStyleInterface $imagelightbox_image_style */
    $imagelightbox_image_style = $this->imageStyleStorage
      ->load($settings['imagelightbox_image_style']);
  }
  $imagelightbox_image_style_responsive = [];
  if (isset($settings['imagelightbox_image_style_responsive'])) {
    foreach ($settings['imagelightbox_image_style_responsive'] as $preset) {
      if ($preset['width']) {
        $imagelightbox_image_style_responsive[$preset['width']] = $this->imageStyleStorage
          ->load($preset['image_style']);
      }
    }
  }

  /** @var \Drupal\file\FileInterface[] $files */
  foreach ($files as $delta => $file) {
    $image_uri = $file
      ->getFileUri();
    $default_url = file_create_url($image_uri);
    $item = $file->_referringItem;
    $item_attributes = $item->_attributes;
    unset($item->_attributes);

    // Prepare caption
    if ($settings['captions_source'] == 'image_alt') {
      $caption = $item
        ->get('alt')
        ->getValue();
    }
    elseif ($settings['captions_source'] == 'image_title') {
      $caption = $item
        ->get('title')
        ->getValue();
    }
    else {
      $caption = '';
    }
    $link_attributes = [
      'class' => 'lightbox',
      'data-imagelightbox' => 'g',
      'data-ilb2-caption' => $caption,
    ];
    $item_attributes = [
      'class' => 'imagelightbox',
    ];
    $elements[$delta] = [
      '#theme' => 'imagelightbox_formatter',
      '#class' => 'imagelightbox',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#link_attributes' => $link_attributes,
      '#image_style' => $settings['image_style'],
      '#url' => empty($imagelightbox_image_style) ? $default_url : $imagelightbox_image_style
        ->buildUrl($image_uri),
      '#cache' => [
        'tags' => $cache_tags,
        'contexts' => isset($cache_contexts) ? $cache_contexts : "",
      ],
    ];
  }
  $elements['#attached']['drupalSettings']['imagelightbox'] = $settings;
  $elements['#attached']['library'][] = 'imagelightbox/formatter';

  //$elements['#attributes']['class'][] = 'imagelightbox';
  if ($settings['inline']) {
    $elements['#attributes']['class'][] = 'container-inline';
  }
  return $elements;
}