You are here

public static function Select2::preRenderOverwrites in Select 2 8

Allows to modify the select2 settings.

1 call to Select2::preRenderOverwrites()
Select2Test::testPreRenderSelect in tests/src/Unit/Element/Select2Test.php
@covers ::preRenderSelect

File

src/Element/Select2.php, line 365

Class

Select2
Provides an select2 form element.

Namespace

Drupal\select2\Element

Code

public static function preRenderOverwrites($element) {
  if (!$element['#multiple']) {
    $empty_option = [
      $element['#empty_value'] => '',
    ];
    $element['#options'] = $empty_option + $element['#options'];
  }

  // Allow to overwrite the default settings and set additional settings.
  foreach ($element["#select2"] as $key => $value) {
    $element['#attributes']['data-select2-config'][$key] = $value;
  }
  $element['#attributes']['data-select2-config'] = Json::encode($element['#attributes']['data-select2-config']);
  return $element;
}