You are here

public function AmpIframeFormatter::viewElements in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/AmpIframeFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpIframeFormatter::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/AmpIframeFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpIframeFormatter::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/AmpIframeFormatter.php, line 32
Contains \Drupal\amp\Plugin\Field\FieldFormatter\AmpIframeFormatter.

Class

AmpIframeFormatter
Plugin implementation of the 'amp_iframe' 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 AmpIframe text element extends that to pass #markup through the
  // amp library for processing an iframe into an amp-iframe.
  foreach ($items as $delta => $item) {
    $elements[$delta] = array(
      '#type' => 'amp_iframe',
      '#text' => $item->value,
      '#format' => $item->format,
      '#langcode' => $item
        ->getLangcode(),
    );
  }
  return $elements;
}