function OptionsWidgetBase::formElement

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase::formElement()

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php, line 67

Class

OptionsWidgetBase
Base class for the 'options_*' widgets.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  // Prepare some properties for the child methods to build the actual form
  // element.
  $this->required = $element['#required'];
  $this->multiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();
  $this->has_value = isset($items[0]->{$this->column});
  // Add our custom validator.
  $element['#element_validate'][] = [
    static::class,
    'validateElement',
  ];
  $element['#key_column'] = $this->column;
  // The rest of the $element is built by child method implementations.
  return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.