You are here

protected static function ElementBase::addSelectField in Select (or other) 4.x

Same name and namespace in other branches
  1. 8 src/Element/ElementBase.php \Drupal\select_or_other\Element\ElementBase::addSelectField()

Adds the 'select' field to the element.

Parameters

array $element: The select or other element.

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

File

src/Element/ElementBase.php, line 114

Class

ElementBase
Base class for select or other form elements.

Namespace

Drupal\select_or_other\Element

Code

protected static function addSelectField(array &$element) {
  if (!empty($element['#other_options'])) {

    // Add "Other" to default values if "Other" was selected.
    $element['#default_value'][] = "select_or_other";
  }
  $element['select'] = [
    '#default_value' => $element['#default_value'],
    '#required' => $element['#required'],
    '#multiple' => $element['#multiple'],
    '#options' => static::addOtherOption($element['#original_options'] ?? $element['#options']),
    '#attributes' => [
      'aria-label' => isset($element['#title']) ? $element['#title'] : $element['#name'],
    ],
    '#weight' => 10,
  ];
}