function OptionsSelectWidget::formElement

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()

Overrides OptionsWidgetBase::formElement

1 method overrides OptionsSelectWidget::formElement()
ModerationStateWidget::formElement in core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php
Returns the form for a single field widget.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php, line 29

Class

OptionsSelectWidget
Plugin implementation of the 'options_select' widget.

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    $element += [
        '#type' => 'select',
        '#options' => $this->getOptions($items->getEntity()),
        '#default_value' => $this->getSelectedOptions($items),
        // Do not display a 'multiple' select box if there is only one option.
'#multiple' => $this->multiple && count($this->options) > 1,
    ];
    return $element;
}

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