class SelectWidget

Plugin implementation of the 'options_select' widget.

Plugin annotation


@Plugin(
  id = "options_select",
  module = "options",
  label = @Translation("Select list"),
  field_types = {
    "list_integer",
    "list_float",
    "list_text"
  },
  multiple_values = TRUE
)

Hierarchy

Expanded class hierarchy of SelectWidget

File

drupal/core/modules/options/lib/Drupal/options/Plugin/field/widget/SelectWidget.php, line 28
Contains \Drupal\options\Plugin\field\widget\SelectWidget.

Namespace

Drupal\options\Plugin\field\widget
View source
class SelectWidget extends OptionsWidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);
    $element += array(
      '#type' => 'select',
      '#options' => $this
        ->getOptions(),
      '#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;
  }

  /**
   * {@inheritdoc}
   */
  protected static 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;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OptionsWidgetBase::$column protected property Abstract over the actual field columns, to allow different field types to reuse those widgets.
OptionsWidgetBase::flattenOptions protected function Flattens an array of allowed values.
OptionsWidgetBase::getOptions protected function Returns the array of options for the widget.
OptionsWidgetBase::getSelectedOptions protected function Determines selected options from the incoming field values.
OptionsWidgetBase::OPTIONS_EMPTY_NONE constant Identifies a 'None' option.
OptionsWidgetBase::OPTIONS_EMPTY_SELECT constant Identifies a 'Select a value' option.
OptionsWidgetBase::validateElement public static function Form validation handler for widget elements.
OptionsWidgetBase::__construct public function Constructs a WidgetBase object. Overrides WidgetBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
SelectWidget::formElement public function Returns the form for a single field widget. Overrides OptionsWidgetBase::formElement
SelectWidget::getEmptyOption protected function Returns the empty option to add to the list of options, if any. Overrides OptionsWidgetBase::getEmptyOption
SelectWidget::sanitizeLabel protected static function Sanitizes a string label to display as an option. Overrides OptionsWidgetBase::sanitizeLabel
SelectWidget::supportsGroups protected function Indicates whether the widgets support optgroups. Overrides OptionsWidgetBase::supportsGroups
WidgetBase::$field protected property The field definition.
WidgetBase::$instance protected property The field instance definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::errorElement public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement(). Overrides WidgetInterface::errorElement 5
WidgetBase::extractFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues(). Overrides WidgetBaseInterface::extractFormValues
WidgetBase::flagErrors public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::flagErrors(). Overrides WidgetBaseInterface::flagErrors
WidgetBase::form public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::form(). Overrides WidgetBaseInterface::form
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::massageFormValues public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() Overrides WidgetInterface::massageFormValues 2
WidgetBase::settingsForm public function Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm(). Overrides WidgetInterface::settingsForm 13
WidgetBase::sortItems protected function Sorts submitted field values according to drag-n-drop reordering.