You are here

public function IframeWidgetBase::formElement in Iframe 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/IframeWidgetBase.php \Drupal\iframe\Plugin\Field\FieldWidget\IframeWidgetBase::formElement()

It is .

Used: (1) at admin edit fields.

Used: (2) at add-story for creation content.

Overrides WidgetInterface::formElement

3 calls to IframeWidgetBase::formElement()
IframeUrlheightWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlheightWidget.php
It is .
IframeUrlWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlWidget.php
It is .
IframeUrlwidthheightWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlwidthheightWidget.php
It is .
3 methods override IframeWidgetBase::formElement()
IframeUrlheightWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlheightWidget.php
It is .
IframeUrlWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlWidget.php
It is .
IframeUrlwidthheightWidget::formElement in src/Plugin/Field/FieldWidget/IframeUrlwidthheightWidget.php
It is .

File

src/Plugin/Field/FieldWidget/IframeWidgetBase.php, line 182

Class

IframeWidgetBase
Plugin implementation base functions.

Namespace

Drupal\iframe\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

  // 1) Shows the "default fields" in the edit-type-field page
  // -- (on_admin_page = true).
  // 2) Edit-fields on the article-edit-page (on_admin_page = false).
  // Global settings.
  $field_settings = $this
    ->getFieldSettings();
  $settings = $this
    ->getSettings() + $field_settings + self::defaultSettings();

  /** @var \Drupal\iframe\Plugin\Field\FieldType\IframeItem $item */
  $item =& $items[$delta];
  $field_definition = $item
    ->getFieldDefinition();
  $on_admin_page = isset($element['#field_parents'][0]) && 'default_value_input' == $element['#field_parents'][0];
  $is_new = $item
    ->getEntity()
    ->isNew();

  // \iframe_debug(0, 'add-story formElement field_setting', $field_settings);
  // \iframe_debug(0, 'add-story formElement setting', $settings);
  $values = $item
    ->toArray();
  if ($is_new || $on_admin_page) {
    foreach ($values as $vkey => $vval) {
      if ($vval !== NULL && $vval !== '') {
        $settings[$vkey] = $vval;
      }
    }
  }
  else {
    if (isset($settings['expose_class']) && $settings['expose_class']) {
      $this->allowedAttributes['class'] = 1;
    }
    foreach ($this->allowedAttributes as $attribute => $attrAllowed) {
      if ($attrAllowed) {
        $settings[$attribute] = $values[$attribute];
      }
    }
  }

  // \iframe_debug(0, 'add-story formElement final setting', $settings);
  foreach ($settings as $attribute => $attrValue) {
    $item
      ->setValue($attribute, $attrValue);
  }
  $element += [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#open' => TRUE,
  ];
  if (!$on_admin_page) {
    $element['#title'] = $field_definition
      ->getLabel();
  }
  $element['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Iframe Title'),
    '#placeholder' => '',
    '#default_value' => $settings['title'],
    '#size' => 80,
    '#maxlength' => 1024,
    '#weight' => 2,
  ];
  $element['url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Iframe URL'),
    '#placeholder' => 'https://',
    '#default_value' => isset($settings['url']) ? $settings['url'] : '',
    '#size' => 80,
    '#maxlength' => 1024,
    '#weight' => 1,
    '#element_validate' => [
      [
        get_class($this),
        'validateUrl',
      ],
    ],
  ];
  $element['width'] = [
    '#title' => $this
      ->t('Iframe Width'),
    '#type' => 'textfield',
    '#default_value' => isset($settings['width']) ? $settings['width'] : '',
    '#description' => self::getSizedescription(),
    '#maxlength' => 4,
    '#size' => 4,
    '#weight' => 3,
    '#element_validate' => [
      [
        get_class($this),
        'validateWidth',
      ],
    ],
  ];
  $element['height'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Iframe Height'),
    '#default_value' => isset($settings['height']) ? $settings['height'] : '',
    '#description' => self::getSizedescription(),
    '#maxlength' => 4,
    '#size' => 4,
    '#weight' => 4,
    '#element_validate' => [
      [
        get_class($this),
        'validateHeight',
      ],
    ],
  ];
  if (isset($settings['expose_class']) && $settings['expose_class']) {
    $element['class'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Additional CSS Class'),
      // ''
      '#default_value' => $settings['class'],
      '#description' => $this
        ->t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
      '#weight' => 5,
    ];
  }

  // \iframe_debug(0, 'formElement', $element);
  return $element;
}