You are here

public function IframeAsurlFormatter::viewElements in Iframe 8

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

Class

IframeAsurlFormatter
Class IframeAsurlFormatter.

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 = '';
    }
    $linktext = empty($item->title) ? $item->url : $item->title;
    $elements[$delta] = [
      '#markup' => Link::fromTextAndUrl($linktext, Url::fromUri($item->url, [
        'title' => $item->title,
      ]))
        ->toString(),
      '#allowed_tags' => [
        'iframe',
        'a',
        'h3',
      ],
    ];
  }
  return $elements;
}