You are here

public static function Select2::attachLibrary in Select2 ALL THE THINGS! 8

Helper function to attach the Select2 library and settings to a given element.

Parameters

array &$element: An render array element.

1 call to Select2::attachLibrary()
Select2::preRenderSelect in src/Select2.php
Render API callback: Apply Select2 to a select element.

File

src/Select2.php, line 89

Class

Select2
Methods for adding Select2 to render elements.

Namespace

Drupal\select2_all

Code

public static function attachLibrary(array &$element) {
  $element['#attached']['library'][] = 'select2_all/drupal.select2';
  $config = \Drupal::config('select2.settings');
  $options = [];
  $language_direction = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getDirection();
  if (LanguageInterface::DIRECTION_RTL === $language_direction) {
    $options['dir'] = 'rtl';
  }
  $element['#attached']['drupalSettings']['select2'] = [
    // 'selector' => $config->get('jquery_selector'),
    // 'minimum_single' => (int) $config->get('minimum_single'),
    // 'minimum_multiple' => (int) $config->get('minimum_multiple'),
    // 'minimum_width' => (int) $config->get('minimum_width'),
    // 'use_relative_width' => (bool) $config->get('use_relative_width'),
    'options' => $options,
  ];
}