You are here

IframeAsurlFormatter.php in Iframe 8

Same filename and directory in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/IframeAsurlFormatter.php

File

src/Plugin/Field/FieldFormatter/IframeAsurlFormatter.php
View source
<?php

namespace Drupal\iframe\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
 * Class IframeAsurlFormatter.
 *
 * @FieldFormatter(
 *  id = "iframe_asurl",
 *  label = @Translation("A link with the given title"),
 *  field_types = {"iframe"}
 * )
 */
class IframeAsurlFormatter extends IframeDefaultFormatter {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

Namesort descending Description
IframeAsurlFormatter Class IframeAsurlFormatter.