You are here

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

File

src/Plugin/Field/FieldFormatter/BxsliderThs.php, line 386

Class

BxsliderThs
BxSlider thumbnail pager.

Namespace

Drupal\bxslider\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $parent_elements = parent::viewElements($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['thumbnail_slider']['thumbnail_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_ths_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());
    $rendering_ths_items[] = $file->_referringItem;
  }

  // A slider's own pager must be disabled, because for pager is used
  // another bxslider.
  $settings['slider']['pager']['pager'] = FALSE;
  $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-ths-' . str_replace('_', '-', $items
    ->getName());
  $bxslider_settings['colorbox'] = $settings['colorbox'];
  $bxslider_settings['thumbnail_slider'] = array_merge($settings['thumbnail_slider']['general'], $settings['thumbnail_slider']['pager'], $settings['thumbnail_slider']['controls'], $settings['thumbnail_slider']['auto'], $settings['thumbnail_slider']['carousel']);
  $bxslider_settings['thumbnail_slider']['thumbnail_style'] = $settings['thumbnail_slider']['thumbnail_style'];

  // Get thumbnail's width.
  $image_style_ths = $this->imageStyleStorage
    ->load($settings['thumbnail_slider']['thumbnail_style']);
  foreach ($image_style_ths
    ->getEffects() as $effect) {
    $thumbnail_width = $effect->configuration['width'];
  }
  $bxslider_settings['thumbnail_slider']['slideWidth'] = $thumbnail_width;
  $element = [
    '#theme' => 'bxslider_ths',
    '#items' => $parent_elements['#items'],
    '#thumbnail_items' => $rendering_ths_items,
    '#settings' => $bxslider_settings,
    '#cache' => [
      'tags' => $cache_tags,
      'contexts' => $cache_contexts,
    ],
  ];

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

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