function OptionsSelectWidget::formElement
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsSelectWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget::formElement()
- 8.9.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 30
Class
- OptionsSelectWidget
- Plugin implementation of the 'options_select' widget.
Namespace
Drupal\Core\Field\Plugin\Field\FieldWidgetCode
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$options = $this->getOptions($items->getEntity());
$selected = $this->getSelectedOptions($items);
// Do not display a 'multiple' select box if there is only one option.
$multiple = $this->multiple && count($options) > 1;
// If the selected option is empty and the field is required, add an option
// to force the user to choose.
if (!isset($options['_none']) && empty($selected) && $this->required && !$multiple) {
// Ensure there is an empty option if the widget needs one.
$empty_label = $this->t('- Select a value -');
$this->sanitizeLabel($empty_label);
$options = [
'_none' => $empty_label,
] + $options;
}
$element += [
'#type' => 'select',
'#options' => $options,
'#default_value' => $selected,
'#multiple' => $multiple,
];
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.