You are here

public function BlazyTextFormatter::viewElements in Blazy 8.2

Same name and namespace in other branches
  1. 7 src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter::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/BlazyTextFormatter.php, line 53

Class

BlazyTextFormatter
Plugin implementation of the 'Blazy Grid Text' formatter.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

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

  // Early opt-out if the field is empty.
  if ($items
    ->isEmpty()) {
    return [];
  }

  // Build the settings.
  $settings = $this
    ->buildSettings();
  $settings['lazy'] = FALSE;
  $settings['langcode'] = $langcode;
  $settings['_grid'] = $settings['_unblazy'] = TRUE;

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

  // Pass to manager for easy updates to all Blazy formatters.
  return $this->formatter
    ->build($build);
}