You are here

public function SliderProFormatter::viewElements in Slider Pro 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/SliderProFormatter.php, line 149

Class

SliderProFormatter
Plugin annotation @FieldFormatter( id = "slider_pro", label = @Translation("Slider Pro"), field_types = { "image" } )

Namespace

Drupal\slider_pro\Plugin\Field\FieldFormatter

Code

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

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return [];
  }
  if (!($optionset = SliderPro::load($this
    ->getSetting('optionset')))) {

    // For some reason, no optionset could be loaded.
    return [];
  }
  $image_style_setting = $this
    ->getSetting('image_style');
  $image_style_thumb_setting = $this
    ->getSetting('image_style_thumb');

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

    /** @var \Drupal\file\FileInterface $file */
    $cache_contexts = [];
    $image_style_cache_tags = Cache::mergeTags($base_cache_tags['image_style'], $file
      ->getCacheTags());
    $image_style_thumb_cache_tags = Cache::mergeTags($base_cache_tags['image_style_thumb'], $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);
    $rows[$delta]['slide']['image'] = [
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#image_style' => $image_style_setting,
      '#cache' => [
        'tags' => $image_style_cache_tags,
        'contexts' => $cache_contexts,
      ],
    ];
    $rows[$delta]['thumb']['image'] = [
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#image_style' => $image_style_thumb_setting,
      '#cache' => [
        'tags' => $image_style_thumb_cache_tags,
        'contexts' => $cache_contexts,
      ],
    ];
  }

  // Build render array.
  $id = 'slider-pro-' . uniqid();
  $content = array(
    '#theme' => 'slider_pro',
    '#rows' => $rows,
    '#uses_thumbnails' => $optionset
      ->hasThumbnails(),
    '#id' => $id,
  );
  $attached = [];

  // JavaScript settings
  $js_settings = array(
    'instances' => array(
      $id => $optionset
        ->toOptionSet(),
    ),
  );

  // Add JS.
  $attached['library'][] = 'slider_pro/slider.pro.load';
  $content['#attached'] = [
    'library' => [
      'slider_pro/slider.pro.load',
    ],
    'drupalSettings' => [
      'sliderPro' => $js_settings,
    ],
  ];
  return $content;
}