You are here

public static function ChosenFormRender::preRenderSelect in Chosen 8.2

Same name and namespace in other branches
  1. 3.0.x src/ChosenFormRender.php \Drupal\chosen\ChosenFormRender::preRenderSelect()

Render API callback: Apply Chosen to a select element.

Parameters

array $element: The element.

Return value

array The element.

File

src/ChosenFormRender.php, line 41

Class

ChosenFormRender

Namespace

Drupal\chosen

Code

public static function preRenderSelect($element) {

  // Exclude chosen 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;
  $chosen_include = \Drupal::config('chosen.settings')
    ->get('chosen_include');
  if ($chosen_include != CHOSEN_INCLUDE_EVERYWHERE && $is_admin == $chosen_include) {
    return $element;
  }

  // If the #chosen FAPI property is set, then add the appropriate class.
  if (isset($element['#chosen'])) {
    if (!empty($element['#chosen'])) {

      // Element has opted-in for Chosen, ensure the library gets added.
      $element['#attributes']['class'][] = 'chosen-enable';
    }
    else {
      $element['#attributes']['class'][] = 'chosen-disable';

      // Element has opted-out of Chosen. Do not add the library now.
      return $element;
    }
  }
  elseif (isset($element['#attributes']['class']) && is_array($element['#attributes']['class'])) {
    if (array_intersect($element['#attributes']['class'], [
      'chosen-disable',
    ])) {

      // Element has opted-out of Chosen. Do not add the library now.
      return $element;
    }
    elseif (array_intersect($element['#attributes']['class'], [
      'chosen-enable',
    ])) {

      // Element has opted-in for Chosen, ensure the library gets added.
    }
  }
  else {

    // Neither the #chosen property was set, nor any chosen classes found.
    // This element still might match the site-wide critera, 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']) && isset($element['#bundle']) && isset($element['#field_name'])) {

      // 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.
  chosen_attach_library($element);

  // Right to Left Support.
  $language_direction = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getDirection();
  if (LanguageInterface::DIRECTION_RTL == $language_direction) {
    $element['#attributes']['class'][] = 'chosen-rtl';
  }
  return $element;
}