Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldWidget / OptionsSelectWidget.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'options_select' widget.
11  *
12  * @FieldWidget(
13  *   id = "options_select",
14  *   label = @Translation("Select list"),
15  *   field_types = {
16  *     "entity_reference",
17  *     "list_integer",
18  *     "list_float",
19  *     "list_string"
20  *   },
21  *   multiple_values = TRUE
22  * )
23  */
24 class OptionsSelectWidget extends OptionsWidgetBase {
25
26   /**
27    * {@inheritdoc}
28    */
29   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
30     $element = parent::formElement($items, $delta, $element, $form, $form_state);
31
32     $element += [
33       '#type' => 'select',
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,
38     ];
39
40     return $element;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function sanitizeLabel(&$label) {
47     // Select form inputs allow unencoded HTML entities, but no HTML tags.
48     $label = Html::decodeEntities(strip_tags($label));
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function supportsGroups() {
55     return TRUE;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function getEmptyLabel() {
62     if ($this->multiple) {
63       // Multiple select: add a 'none' option for non-required fields.
64       if (!$this->required) {
65         return t('- None -');
66       }
67     }
68     else {
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) {
73         return t('- None -');
74       }
75       if (!$this->has_value) {
76         return t('- Select a value -');
77       }
78     }
79   }
80
81 }