3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Plugin implementation of the 'options_select' widget.
13 * id = "options_select",
14 * label = @Translation("Select list"),
21 * multiple_values = TRUE
24 class OptionsSelectWidget extends OptionsWidgetBase {
29 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
30 $element = parent::formElement($items, $delta, $element, $form, $form_state);
34 '#options' => $this->getOptions($items->getEntity()),
35 '#default_value' => $this->getSelectedOptions($items),
36 // Do not display a 'multiple' select box if there is only one option.
37 '#multiple' => $this->multiple && count($this->options) > 1,
46 protected function sanitizeLabel(&$label) {
47 // Select form inputs allow unencoded HTML entities, but no HTML tags.
48 $label = Html::decodeEntities(strip_tags($label));
54 protected function supportsGroups() {
61 protected function getEmptyLabel() {
62 if ($this->multiple) {
63 // Multiple select: add a 'none' option for non-required fields.
64 if (!$this->required) {
69 // Single select: add a 'none' option for non-required fields,
70 // and a 'select a value' option for required fields that do not come
71 // with a value selected.
72 if (!$this->required) {
75 if (!$this->has_value) {
76 return t('- Select a value -');