View source
<?php
namespace Drupal\simple_iframe\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class SimpleIframeWidgetType extends WidgetBase {
public static function defaultSettings() {
return [
'size' => 100,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = [];
$elements['size'] = [
'#type' => 'number',
'#title' => t('Size of textfield'),
'#default_value' => $this
->getSetting('size'),
'#required' => TRUE,
'#min' => 1,
];
return $elements;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item = $items[$delta];
$field_settings = $this
->getFieldSettings();
$settings = $this
->getSettings() + $field_settings;
$element['url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Iframe URL'),
'#description' => $this
->t('Set the source of the iframe'),
'#placeholder' => '//',
'#default_value' => isset($items[$delta]->url) ? $items[$delta]->url : NULL,
'#size' => $this
->getSetting('size'),
'#maxlength' => 2000,
'#weight' => 1,
'#required' => $element['#required'],
];
$width = isset($item->width) && !empty($item->width) ? $item->width : (isset($settings['width']) ? $settings['width'] : NULL);
$element['width'] = [
'#title' => $this
->t('Width'),
'#description' => $this
->t('Set a number or %'),
'#type' => 'textfield',
'#default_value' => $width,
'#required' => $element['#required'],
'#maxlength' => 8,
'#size' => 6,
'#weight' => 3,
];
$height = isset($item->height) && !empty($item->height) ? $item->height : (isset($settings['height']) ? $settings['height'] : NULL);
$element['height'] = [
'#type' => 'textfield',
'#title' => $this
->t('Height'),
'#description' => $this
->t('Set a number'),
'#default_value' => $height,
'#maxlength' => 8,
'#size' => 6,
'#weight' => 4,
'#required' => $element['#required'],
];
return $element;
}
}