ChosenFormRender.php in Chosen 8.2
File
src/ChosenFormRender.php
View source
<?php
namespace Drupal\chosen;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Language\LanguageInterface;
if (interface_exists('\\Drupal\\Core\\Security\\TrustedCallbackInterface')) {
interface TrustedCallbackInterface extends \Drupal\Core\Security\TrustedCallbackInterface {
}
}
else {
interface TrustedCallbackInterface {
}
}
class ChosenFormRender implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRenderSelect',
'preRenderDateCombo',
'preRenderSelectOther',
];
}
public static function preRenderSelect($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;
$chosen_include = \Drupal::config('chosen.settings')
->get('chosen_include');
if ($chosen_include != CHOSEN_INCLUDE_EVERYWHERE && $is_admin == $chosen_include) {
return $element;
}
if (isset($element['#chosen'])) {
if (!empty($element['#chosen'])) {
$element['#attributes']['class'][] = 'chosen-enable';
}
else {
$element['#attributes']['class'][] = 'chosen-disable';
return $element;
}
}
elseif (isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) {
if (array_intersect($element['#attributes']['class'], [
'chosen-disable',
])) {
return $element;
}
elseif (array_intersect($element['#attributes']['class'], [
'chosen-enable',
])) {
}
}
else {
}
if (isset($element['#field_name']) && !empty($element['#multiple'])) {
unset($element['#options']['_none']);
if (isset($element['#entity_type']) && isset($element['#bundle']) && isset($element['#field_name'])) {
$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;
}
}
}
chosen_attach_library($element);
$language_direction = \Drupal::languageManager()
->getCurrentLanguage()
->getDirection();
if (LanguageInterface::DIRECTION_RTL == $language_direction) {
$element['#attributes']['class'][] = 'chosen-rtl';
}
return $element;
}
public static function preRenderDateCombo($element) {
if (isset($element['#chosen'])) {
chosen_element_apply_property_recursive($element, $element['#chosen']);
}
return $element;
}
public static function preRenderSelectOther($element) {
if ($element['#select_type'] == 'select' && isset($element['#chosen'])) {
$element['select']['#chosen'] = $element['#chosen'];
}
return $element;
}
}