You are here

public function FlexsliderEntityFormatter::viewElements in Flex Slider 8.2

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 EntityReferenceEntityFormatter::viewElements

File

flexslider_fields/src/Plugin/Field/FieldFormatter/FlexsliderEntityFormatter.php, line 51

Class

FlexsliderEntityFormatter
Plugin implementation of the 'flexslider_entity' formatter.

Namespace

Drupal\flexslider_fields\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entities = parent::viewElements($items, $langcode);

  // Bail out if there are no entities to render.
  if (empty($entities)) {
    return [];
  }
  $formatter_settings = $this
    ->getSettings();

  // Get cache tags for the option set.
  if ($optionset = Flexslider::load($formatter_settings['optionset'])) {
    $cache_tags = $optionset
      ->getCacheTags();
  }
  else {
    $cache_tags = [];
  }
  $items = [];
  foreach ($entities as $delta => &$entity) {

    // Merge in the cache tags.
    if ($cache_tags) {
      $entity['#cache']['tags'] = Cache::mergeTags($entity['#cache']['tags'], $cache_tags);
    }

    // Prepare the slide item render array.
    $item = [];
    $item['slide'] = $entity;
    $items[$delta] = $item;
  }
  return [
    '#theme' => 'flexslider',
    '#flexslider' => [
      'settings' => $formatter_settings,
      'items' => $items,
    ],
    // @todo we probably want a twig template for this.
    '#prefix' => '<div class="flexslider-field-wrapper">',
    '#suffix' => '</div>',
  ];
}