You are here

protected static function Buttons::addEmptyOption in Select (or other) 8

Same name and namespace in other branches
  1. 4.x src/Element/Buttons.php \Drupal\select_or_other\Element\Buttons::addEmptyOption()

Adds an empty option to the select element if required.

Parameters

array $element: The select or other element.

1 call to Buttons::addEmptyOption()
Buttons::processSelectOrOther in src/Element/Buttons.php
Render API callback: Expands the select_or_other element type.

File

src/Element/Buttons.php, line 81

Class

Buttons
Provides a form element with buttons and other option.

Namespace

Drupal\select_or_other\Element

Code

protected static function addEmptyOption(array &$element) {
  if (!isset($element['#no_empty_option']) || !$element['#no_empty_option']) {
    if (!$element['#multiple'] && !$element['#required'] && !empty($element['#default_value'])) {
      $element['select']['#options'] = [
        '' => t('- None -'),
      ] + $element['select']['#options'];
    }
    else {
      $element['select']['#value'] = [];
    }
  }
}