You are here

public function AmpTextFormatter::viewElements in Accelerated Mobile Pages (AMP) 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/AmpTextFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpTextFormatter::viewElements()
  2. 8 src/Plugin/Field/FieldFormatter/AmpTextFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpTextFormatter::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 TextDefaultFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/AmpTextFormatter.php, line 27

Class

AmpTextFormatter
Plugin implementation of the 'amp_text' formatter.

Namespace

Drupal\amp\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();

  // The ProcessedText element already handles cache context & tag bubbling.
  // @see \Drupal\filter\Element\ProcessedText::preRenderText()
  // The AmpProcessed text element extends that to pass #markup through the
  // amp library for processing markup into AMP HTML.
  foreach ($items as $delta => $item) {
    $elements[$delta] = array(
      '#type' => 'amp_processed_text',
      '#text' => $item->value,
      '#format' => $item->format,
      '#langcode' => $item
        ->getLangcode(),
    );
  }
  return $elements;
}