Skip to content

Instantly share code, notes, and snippets.

@mohit-rocks
Created December 15, 2022 12:56
Show Gist options
  • Save mohit-rocks/2fa23ec3a79ee13ac7c66fc5fcca2317 to your computer and use it in GitHub Desktop.
Save mohit-rocks/2fa23ec3a79ee13ac7c66fc5fcca2317 to your computer and use it in GitHub Desktop.
Iframe module field plugins
<?php
namespace Drupal\iframe\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Class IframeAllAttributesFormatter.
*
* @FieldFormatter(
* id = "iframe_with_all_attributes",
* label = @Translation("Iframe (without title) and all attributes"),
* field_types = {"iframe"}
* )
*/
class IframeAllAttributesFormatter extends IframeDefaultFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
// settings from type
$settings = $this->getSettings();
// field_settings on concrete field
$field_settings = $this->getFieldSettings();
$allow_attributes = [ 'url', 'width', 'height', 'title', 'allowfullscreen', 'sandbox', 'referrerpolicy' ];
foreach ($items as $delta => $item) {
if (empty($item->url)) {
continue;
}
if (!isset($item->title)) {
$item->title = '';
}
foreach($field_settings as $field_key => $field_val) {
if (in_array($field_key, $allow_attributes)) {
continue;
}
$item->{$field_key} = $field_val;
}
$elements[$delta] = self::iframeIframe('', $item->url, $item);
// Add additional attributes.
if (!empty($item->allowfullscreen)) {
$elements[$delta]['#attributes']->setAttribute('allowfullscreen', (bool) $item->allowfullscreen);
}
if (!empty($item->sandbox)) {
$elements[$delta]['#attributes']->setAttribute('sandbox', $item->sandbox);
}
if (!empty($item->referrerpolicy)) {
$elements[$delta]['#attributes']->setAttribute('referrerpolicy', $item->referrerpolicy);
}
// Tokens can be dynamic, so it's not cacheable.
if (isset($settings['tokensupport']) && $settings['tokensupport']) {
$elements[$delta]['cache'] = ['max-age' => 0];
}
}
return $elements;
}
}
<?php
namespace Drupal\iframe\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'iframe_urlwidthheigth' widget.
*
* @FieldWidget(
* id = "iframe_url_with_all_attributest",
* label = @Translation("URL with all the attributes."),
* field_types = {"iframe"}
* )
*/
class IframeUrlAllAttributesWidget extends IframeUrlwidthheightWidget {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$values = $items[$delta]->toArray();
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['sandbox'] = [
'#type' => 'textfield',
'#title' => $this->t('Sandbox'),
'#default_value' => $values['sandbox'] ?? '',
'#description' => 'Applies extra restrictions to the content in the frame. The value of the attribute can either be empty to apply all restrictions, or space-separated tokens to lift particular restrictions',
'#maxlength' => 255,
'#size' => 60,
'#weight' => 5,
];
$element['referrerpolicy'] = [
'#type' => 'textfield',
'#title' => $this->t('Referer Policy'),
'#default_value' => $values['referrerpolicy'] ?? '',
'#description' => "Indicates which referrer to send when fetching the frame's resource",
'#maxlength' => 255,
'#size' => 60,
'#weight' => 6,
];
$element['allowfullscreen'] = [
'#type' => 'select',
'#title' => $this->t('Allow fullscreen'),
'#options' => [
'0' => $this->t('false'),
'1' => $this->t('true'),
],
'#weight' => 7,
'#default_value' => $values['allowfullscreen'] ?? '0',
'#description' => $this->t('Allow fullscreen for iframe. The iframe can activate fullscreen mode by calling the requestFullscreen() method.'),
];
return $element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment