You are here

public function TwentyTwentyFieldFormatter::viewElements in ZURB TwentyTwenty 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/TwentyTwentyFieldFormatter.php \Drupal\zurb_twentytwenty\Plugin\Field\FieldFormatter\TwentyTwentyFieldFormatter::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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/TwentyTwentyFieldFormatter.php, line 227
Contains Drupal\zurb_twentytwenty\Plugin\Field\FieldFormatter\TwentyTwentyFieldFormatter.

Class

TwentyTwentyFieldFormatter
Plugin implementation of the 'twentytwenty_field_formatter' formatter.

Namespace

Drupal\zurb_twentytwenty\Plugin\Field\FieldFormatter

Code

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

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  $image_style_setting = $this
    ->getSetting('image_style');
  $elements['#attached']['drupalSettings']['twentytwenty']['default_offset_pct'] = $this
    ->getSetting('default_offset_pct');
  $elements['#attached']['drupalSettings']['twentytwenty']['orientation'] = $this
    ->getSetting('orientation');
  $elements['#attached']['drupalSettings']['twentytwenty']['before_label'] = $this
    ->getSetting('before_label');
  $elements['#attached']['drupalSettings']['twentytwenty']['after_label'] = $this
    ->getSetting('after_label');
  $elements['#attached']['drupalSettings']['twentytwenty']['no_overlay'] = $this
    ->getSetting('no_overlay');
  $elements['#attached']['drupalSettings']['twentytwenty']['move_slider_on_hover'] = $this
    ->getSetting('move_slider_on_hover');
  $elements['#attached']['drupalSettings']['twentytwenty']['move_with_handle_only'] = $this
    ->getSetting('move_with_handle_only');
  $elements['#attached']['drupalSettings']['twentytwenty']['click_to_move'] = $this
    ->getSetting('click_to_move');

  // Collect cache tags to be added for each item in the field.
  $cache_tags = array();
  if (!empty($image_style_setting)) {
    $image_style = $this->imageStyleStorage
      ->load($image_style_setting);
    $cache_tags = $image_style
      ->getCacheTags();
  }
  foreach ($files as $delta => $file) {

    /** @var \Drupal\file\FileInterface $file */
    $image_uri = $file
      ->getFileUri();
    $url = Url::fromUri(file_create_url($image_uri));
    $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);
    $images[$delta] = array(
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#image_style' => $image_style_setting,
      '#url' => $url,
      '#cache' => array(
        'tags' => $cache_tags,
      ),
    );
  }
  return array(
    '#theme' => 'zurb_twentytwenty',
    '#images' => render($images),
    '#attached' => [
      'drupalSettings' => [
        'twentytwenty' => [
          'default_offset_pct' => $this
            ->getSetting('default_offset_pct'),
          'orientation' => $this
            ->getSetting('orientation'),
          'before_label' => $this
            ->getSetting('before_label'),
          'after_label' => $this
            ->getSetting('after_label'),
          'no_overlay' => $this
            ->getSetting('no_overlay'),
          'move_slider_on_hover' => $this
            ->getSetting('move_slider_on_hover'),
          'move_with_handle_only' => $this
            ->getSetting('move_with_handle_only'),
          'click_to_move' => $this
            ->getSetting('click_to_move'),
        ],
      ],
    ],
  );
}