public static function Select2::preRenderSelect in Select2 ALL THE THINGS! 8
Render API callback: Apply Select2 to a select element.
Parameters
array $element: The element.
Return value
array The element.
File
- src/
Select2.php, line 25
Class
- Select2
- Methods for adding Select2 to render elements.
Namespace
Drupal\select2_allCode
public static function preRenderSelect(array $element) {
// Exclude Select2 from theme other than admin.
$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;
// $select2_include = \Drupal::config('select2.settings')->get('select2_include');
// if ($select2_include != SELECT2_INCLUDE_EVERYWHERE && $is_admin == $select2_include) {
// return $element;
// }
// If the #select2 FAPI property is set, then add the appropriate class.
if (isset($element['#select2'])) {
if (!empty($element['#select2'])) {
// Element has opted-in for Select2, ensure the library gets added.
$element['#attributes']['class'][] = 'select2-enable';
}
else {
$element['#attributes']['class'][] = 'select2-disable';
// Element has opted-out of Select2. Do not add the library now.
return $element;
}
}
elseif (isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) {
if (array_intersect($element['#attributes']['class'], [
'select2-disable',
])) {
// Element has opted-out of Select2. Do not add the library now.
return $element;
}
elseif (array_intersect($element['#attributes']['class'], [
'select2-enable',
])) {
// Element has opted-in for Select2, ensure the library gets added.
}
}
else {
// Neither the #select property was set, nor any select2 classes found.
// This element still might match the site-wide criteria, so add the library.
}
if (isset($element['#field_name']) && !empty($element['#multiple'])) {
// Remove '_none' from multi-select options.
unset($element['#options']['_none']);
if (isset($element['#entity_type'], $element['#bundle'])) {
// Set data-cardinality for fields that aren't unlimited.
$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;
}
}
}
// Attach the library.
static::attachLibrary($element);
return $element;
}