You are here

public function IframeAsurlwithuriFormatter::viewElements in Iframe 8

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

Class

IframeAsurlwithuriFormatter
Class IframeAsurlwithuriFormatter.

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