View source
<?php
namespace Drupal\iframe\Plugin\Field\FieldFormatter;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Render\Markup;
class IframeDefaultFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'url' => '',
'title' => '',
'width' => '',
'height' => '',
'class' => '',
'frameborder' => '0',
'scrolling' => '',
'transparency' => '0',
'tokensupport' => '0',
'allowfullscreen' => '0',
] + parent::defaultSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$field_settings = $this
->getFieldSettings();
$settings = $this
->getSettings();
$entity = $items
->getEntity();
\iframe_debug(3, __METHOD__, $field_settings);
\iframe_debug(3, __METHOD__, $settings);
\iframe_debug(3, __METHOD__, $entity);
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(self::iframeIframe($item->title, $item->url, $item)),
'#allowed_tags' => [
'iframe',
'a',
'h3',
'style',
],
];
if (isset($settings['tokensupport']) && $settings['tokensupport']) {
$elements[$delta]['cache'] = [
'max-age' => 0,
];
}
}
return $elements;
}
public static function iframeIframe($text, $path, $item) {
$options = [];
$options['width'] = empty($item->width) ? '100%' : $item->width;
$options['height'] = empty($item->height) ? '701' : $item->height;
$allow = [];
$style = '';
$itemName = $item
->getFieldDefinition()
->getName();
$itemParentId = $item
->getParent()
->getParent()
->getEntity()
->ID();
if (!empty($item->frameborder) && $item->frameborder > 0) {
$style .= '/*frameborder*/ border-width:2px;';
}
else {
$style .= '/*frameborder*/ border-width:0;';
}
if (!empty($item->scrolling)) {
if ($item->scrolling == 'yes') {
$style .= '/*scrolling*/ overflow:scroll;';
}
elseif ($item->scrolling == 'no') {
$style .= '/*scrolling*/ overflow:hidden;';
}
else {
$style .= '/*scrolling*/ overflow:auto;';
}
}
if (!empty($item->transparency) && $item->transparency > 0) {
$style .= '/*transparency*/ background-color:transparent;';
}
$htmlid = 'iframe-' . $itemName . '-' . $itemParentId;
if (property_exists($item, 'htmlid') && $item->htmlid !== null && !empty($item->htmlid)) {
$htmlid = $item->htmlid;
}
$htmlid = preg_replace('#[^A-Za-z0-9\\-\\_]+#', '-', $htmlid);
$options['id'] = $options['name'] = $htmlid;
$options['class'] = empty($item->class) ? '' : $item->class;
$options['title'] = empty($item->title) ? '' : $item->title;
if (!empty($options['title']) && strpos($options['title'], '<') !== FALSE) {
$options['title'] = strip_tags($options['title']);
}
if (!empty($item->allowfullscreen) && $item->allowfullscreen) {
$allow[] = 'fullscreen';
}
$allow[] = 'autoplay';
$allow[] = 'camera';
$allow[] = 'microphone';
$allow[] = 'payment';
$allow[] = 'accelerometer';
$allow[] = 'geolocation';
$allow[] = 'encrypted-media';
$allow[] = 'gyroscope';
$options['allow'] = implode(';', $allow);
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$tokensupport = $item
->getTokenSupport();
$tokencontext = [
'user' => \Drupal::currentUser(),
];
if (isset($GLOBALS['node'])) {
$tokencontext['node'] = $GLOBALS['node'];
}
if ($tokensupport > 0) {
$text = \Drupal::token()
->replace($text, $tokencontext);
}
if ($tokensupport > 1) {
$path = \Drupal::token()
->replace($path, $tokencontext);
}
}
$options_link = [];
$options_link['attributes'] = [];
$options_link['attributes']['title'] = $options['title'];
try {
$srcuri = Url::fromUri($path, $options_link);
$src = $srcuri
->toString();
$options['src'] = $src;
$drupal_attributes = new Attribute($options);
$output = '<div class="' . (empty($options['class']) ? '' : new HtmlEscapedText($options['class'])) . '">' . (empty($text) ? '' : '<h3 class="iframe_title">' . (isset($options['html']) && $options['html'] ? $text : new HtmlEscapedText($text)) . '</h3>') . '<style type="text/css">iframe#' . $htmlid . ' {' . $style . '}</style>' . "\n" . ' <iframe ' . $drupal_attributes
->__toString() . '>' . t('Your browser does not support iframes, but you can use the following link:') . ' ' . Link::fromTextAndUrl('Link', $srcuri)
->toString() . '</iframe>' . '</div>';
return $output;
} catch (\Exception $excep) {
watchdog_exception(__METHOD__, $excep);
return '';
}
}
}