You are here

public function SlickTextFormatter::viewElements in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/SlickTextFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickTextFormatter::viewElements()
  2. 7.3 src/Plugin/Field/FieldFormatter/SlickTextFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickTextFormatter::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/SlickTextFormatter.php, line 69

Class

SlickTextFormatter
Plugin implementation of the 'Slick Text' formatter.

Namespace

Drupal\slick\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  // Early opt-out if the field is empty.
  if ($items
    ->isEmpty()) {
    return [];
  }
  $settings = $this
    ->getSettings();
  $settings['vanilla'] = TRUE;

  // Build the settings.
  $build = [
    'settings' => $settings,
  ];
  $this->formatter
    ->buildSettings($build, $items);

  // The ProcessedText element already handles cache context & tag bubbling.
  // @see \Drupal\filter\Element\ProcessedText::preRenderText()
  foreach ($items as $key => $item) {
    $element = [
      '#type' => 'processed_text',
      '#text' => $item->value,
      '#format' => $item->format,
      '#langcode' => $item
        ->getLangcode(),
    ];
    $build['items'][$key] = $element;
    unset($element);
  }
  return $this
    ->manager()
    ->build($build);
}