protected function SelectWidget::getEmptyOption

Returns the empty option to add to the list of options, if any.

Return value

string|null Either static::OPTIONS_EMPTY_NONE, static::OPTIONS_EMPTY_SELECT, or NULL.

Overrides OptionsWidgetBase::getEmptyOption

File

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

Class

SelectWidget
Plugin implementation of the 'options_select' widget.

Namespace

Drupal\options\Plugin\field\widget

Code

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;
    }
  }
}