View source
<?php
namespace Drupal\select2_all;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\field\Entity\FieldConfig;
class Select2 implements TrustedCallbackInterface {
public static function preRenderSelect(array $element) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$admin_theme = \Drupal::config('system.theme')
->get('admin');
$is_admin_path = \Drupal::service('router.admin_context')
->isAdminRoute();
$is_admin = $is_admin_path || $theme == $admin_theme;
if (isset($element['#select2'])) {
if (!empty($element['#select2'])) {
$element['#attributes']['class'][] = 'select2-enable';
}
else {
$element['#attributes']['class'][] = 'select2-disable';
return $element;
}
}
elseif (isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) {
if (array_intersect($element['#attributes']['class'], [
'select2-disable',
])) {
return $element;
}
elseif (array_intersect($element['#attributes']['class'], [
'select2-enable',
])) {
}
}
else {
}
if (isset($element['#field_name']) && !empty($element['#multiple'])) {
unset($element['#options']['_none']);
if (isset($element['#entity_type'], $element['#bundle'])) {
$field = FieldConfig::loadByName($element['#entity_type'], $element['#bundle'], $element['#field_name'])
->getFieldStorageDefinition();
$cardinality = $field
->getCardinality();
if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && $cardinality > 1) {
$element['#attributes']['data-cardinality'] = $cardinality;
}
}
}
static::attachLibrary($element);
return $element;
}
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'] = [
'options' => $options,
];
}
public static function preRenderDateCombo(array $element) {
if (isset($element['#select2'])) {
static::elementApplyPropertyRecursive($element, $element['#select2']);
}
return $element;
}
public static function preRenderSelectOrOther(array $element) {
if ($element['#select_type'] === 'select' && isset($element['#select2'])) {
$element['select']['#select2'] = $element['#select2'];
}
return $element;
}
public static function elementApplyPropertyRecursive(array &$element, $select2_value = NULL) {
if (!isset($select2_value)) {
if (isset($element['#select2'])) {
$select2_value = $element['#select2'];
}
else {
return;
}
}
if (isset($element['#type']) && $element['#type'] === 'select') {
$element['#chosen'] = $select2_value;
}
foreach (Element::children($element) as $key) {
static::elementApplyPropertyRecursive($element[$key], $select2_value);
}
}
public static function trustedCallbacks() {
return [
'preRenderSelect',
'preRenderDateCombo',
'preRenderSelectOrOther',
];
}
}