You are here

public function IframeOnlyFormatter::viewElements in Iframe 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/IframeOnlyFormatter.php \Drupal\iframe\Plugin\Field\FieldFormatter\IframeOnlyFormatter::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 IframeDefaultFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/IframeOnlyFormatter.php, line 22

Class

IframeOnlyFormatter
Class IframeOnlyFormatter.

Namespace

Drupal\iframe\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    if (empty($item->url)) {
      continue;
    }
    if (!(property_exists($item, 'title') && $item->title !== null)) {
      $item->title = '';
    }
    $elements[$delta] = [
      '#markup' => Markup::create(IframeDefaultFormatter::iframeIframe('', $item->url, $item)),
      '#allowed_tags' => [
        'iframe',
        'a',
        'h3',
        'style',
      ],
    ];

    // Tokens can be dynamic, so its not cacheable.
    if (isset($settings['tokensupport']) && $settings['tokensupport']) {
      $elements[$delta]['cache'] = [
        'max-age' => 0,
      ];
    }
  }
  return $elements;
}