Newer
Older
<?php
/**
* @file
* Contains \Drupal\options\Plugin\field\widget\SelectWidget.
*/
namespace Drupal\options\Plugin\field\widget;
use Drupal\field\Annotation\FieldWidget;
use Drupal\Core\Annotation\Translation;
Dries Buytaert
committed
use Drupal\Core\Entity\Field\FieldInterface;
/**
* Plugin implementation of the 'options_select' widget.
*
* @FieldWidget(
* id = "options_select",
* label = @Translation("Select list"),
* field_types = {
* "list_integer",
* "list_float",
* "list_text"
* },
* multiple_values = TRUE
* )
*/
class SelectWidget extends OptionsWidgetBase {
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function formElement(FieldInterface $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
$element += array(
'#type' => 'select',
Alex Pott
committed
'#options' => $this->getOptions($items[$delta]),
'#default_value' => $this->getSelectedOptions($items, $delta),
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Do not display a 'multiple' select box if there is only one option.
'#multiple' => $this->multiple && count($this->options) > 1,
);
return $element;
}
/**
* {@inheritdoc}
*/
static protected function sanitizeLabel(&$label) {
// Select form inputs allow unencoded HTML entities, but no HTML tags.
$label = strip_tags($label);
}
/**
* {@inheritdoc}
*/
protected function supportsGroups() {
return TRUE;
}
/**
* {@inheritdoc}
*/
protected function getEmptyOption() {
if ($this->multiple) {
// Multiple select: add a 'none' option for non-required fields.
if (!$this->required) {
return static::OPTIONS_EMPTY_NONE;
}
}
else {
// Single select: add a 'none' option for non-required fields,
// and a 'select a value' option for required fields that do not come
// with a value selected.
if (!$this->required) {
return static::OPTIONS_EMPTY_NONE;
}
if (!$this->has_value) {
return static::OPTIONS_EMPTY_SELECT;
}
}
}
}