IframeAsurlFormatter.php in Iframe 8
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 extends IframeDefaultFormatter {
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;
}
}