You are here

public function BaguetteboxFormatter::viewElements in baguetteBox.js 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 ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/BaguetteboxFormatter.php, line 178

Class

BaguetteboxFormatter
Plugin implementation of the 'baguettebox' formatter.

Namespace

Drupal\baguettebox\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();

  // Collect cache tags to be added for each item in the field.
  $cache_tags = [];
  if ($settings['image_style']) {
    $image_style = $this->imageStyleStorage
      ->load($settings['image_style']);
    $cache_tags = $image_style
      ->getCacheTags();
  }

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

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

  /** @var \Drupal\file\FileInterface[] $files */
  foreach ($files as $delta => $file) {
    $cache_contexts = [];
    $cache_contexts[] = 'url.site';
    $cache_tags = Cache::mergeTags($cache_tags, $file
      ->getCacheTags());

    // Extract field item attributes for the theme function, and unset them
    // from the $item so that the field template does not re-render them.
    $item = $file->_referringItem;
    $item_attributes = $item->_attributes;
    unset($item->_attributes);
    $image_uri = $file
      ->getFileUri();
    $default_url = file_create_url($image_uri);
    $link_attributes = [];
    foreach ($baguette_image_style_responsive as $width => $image_style) {
      $link_attributes['data-at-' . $width] = $image_style ? $image_style
        ->buildUrl($image_uri) : $default_url;
    }
    $elements[$delta] = [
      '#theme' => 'baguettebox_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#link_attributes' => $link_attributes,
      '#image_style' => $settings['image_style'],
      '#url' => empty($baguette_image_style) ? $default_url : $baguette_image_style
        ->buildUrl($image_uri),
      '#cache' => [
        'tags' => $cache_tags,
        'contexts' => $cache_contexts,
      ],
    ];
  }
  $elements['#attached']['drupalSettings']['baguettebox'] = $settings;
  $elements['#attached']['library'][] = 'baguettebox/formatter';
  $elements['#attributes']['class'][] = 'baguettebox';
  if ($settings['inline']) {
    $elements['#attributes']['class'][] = 'container-inline';
  }
  return $elements;
}