You are here

protected function EntityReferenceViewsOptionsSelectWidget::getEmptyLabel in Entity Reference Views Select 8

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

Return value

string|null Either a label of the empty option, or NULL.

Overrides OptionsWidgetBase::getEmptyLabel

1 call to EntityReferenceViewsOptionsSelectWidget::getEmptyLabel()
EntityReferenceViewsOptionsSelectWidget::formElement in src/Plugin/Field/FieldWidget/EntityReferenceViewsOptionsSelectWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/EntityReferenceViewsOptionsSelectWidget.php, line 154

Class

EntityReferenceViewsOptionsSelectWidget
Plugin implementation of the 'erviews_options_select' widget.

Namespace

Drupal\entity_reference_views_select\Plugin\Field\FieldWidget

Code

protected function getEmptyLabel() {
  if ($this->multiple) {

    // Multiple select: add a 'none' option for non-required fields.
    if (!$this->required) {
      return $this
        ->getSetting('empty_value') ?? $this
        ->t('- 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 $this
        ->getSetting('empty_value') ?? $this
        ->t('- None -');
    }
    if (!$this->has_value) {
      return $this
        ->getSetting('empty_value') ?? $this
        ->t('- Select a value -');
    }
  }
}