You are here

public function Bxslider::viewElements in BxSlider 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

1 call to Bxslider::viewElements()
BxsliderThs::viewElements in src/Plugin/Field/FieldFormatter/BxsliderThs.php
Builds a renderable array for a field value.
1 method overrides Bxslider::viewElements()
BxsliderThs::viewElements in src/Plugin/Field/FieldFormatter/BxsliderThs.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/Bxslider.php, line 695

Class

Bxslider
BxSlider.

Namespace

Drupal\bxslider\Plugin\Field\FieldFormatter

Code

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

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  $image_style_setting = $settings['slider']['image_style'];

  // Collect cache tags to be added for each item in the field.
  $base_cache_tags = [];
  if (!empty($image_style_setting)) {
    $image_style = $this->imageStyleStorage
      ->load($image_style_setting);
    $base_cache_tags = $image_style
      ->getCacheTags();
  }
  $rendering_items = [];
  foreach ($files as $delta => $file) {
    $cache_contexts = [];
    if (isset($link_file)) {
      $image_uri = $file
        ->getFileUri();
      $url = Url::fromUri(file_create_url($image_uri));
      $cache_contexts[] = 'url.site';
    }
    $cache_tags = Cache::mergeTags($base_cache_tags, $file
      ->getCacheTags());
    $item = $file->_referringItem;
    $item_attributes = $item->_attributes;
    unset($item->_attributes);
    $rendering_items[$delta]['item'] = $item;
    $rendering_items[$delta]['item_attributes'] = $item_attributes;
    $rendering_items[$delta]['entity'] = $items
      ->getEntity();
  }

  // BxSlider settings must be flat (on one level).
  $bxslider_settings['bxslider'] = array_merge($settings['slider']['general'], $settings['slider']['pager'], $settings['slider']['controls'], $settings['slider']['auto'], $settings['slider']['carousel']);
  $bxslider_settings['image_style'] = $settings['slider']['image_style'];
  $bxslider_settings['slider_id'] = 'bxslider-' . str_replace('_', '-', $items
    ->getName());
  $bxslider_settings['colorbox'] = $settings['colorbox'];
  $element = [
    '#theme' => 'bxslider',
    '#items' => $rendering_items,
    '#settings' => $bxslider_settings,
    '#cache' => [
      'tags' => $cache_tags,
      'contexts' => $cache_contexts,
    ],
  ];

  // Attach library.
  $element['#attached']['library'][] = 'bxslider/jquery.bxslider';

  // Attach settings.
  $this
    ->sliderSettingsFixIntegerValues($bxslider_settings);
  $element['#attached']['drupalSettings']['bxslider'][$bxslider_settings['slider_id']] = $bxslider_settings;
  return $element;
}