You are here

function chosen_pre_render_select in Chosen 7.2

Same name and namespace in other branches
  1. 7.3 chosen.module \chosen_pre_render_select()

Render API callback: Apply Chosen to a select element.

1 string reference to 'chosen_pre_render_select'
chosen_element_info_alter in ./chosen.module
Implements hook_element_info_alter().

File

./chosen.module, line 199
General functions and hook implementations.

Code

function chosen_pre_render_select($element) {

  // Exclude chosen from theme other than admin.
  global $theme, $language;
  $is_admin = path_is_admin(current_path()) || current_path() == 'system/ajax' || $theme == variable_get('admin_theme');
  $chosen_include = variable_get('chosen_include', CHOSEN_INCLUDE_EVERYWHERE);
  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'], array(
      'chosen-disable',
    ))) {

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

      // Element has opted-in for Chosen, ensure the library gets added.
      // @todo Remove support for the deprecated chosen-widget class.
    }
  }
  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']);
    $field = field_info_field($element['#field_name']);
    if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field['cardinality'] > 1) {
      $element['#attributes']['data-cardinality'] = $field['cardinality'];
    }
  }

  // Right to Left Support.
  if ($language->direction == 1) {
    $element['#attributes']['class'][] = 'chosen-rtl';
  }
  $element['#attached']['library'][] = array(
    'chosen',
    'drupal.chosen',
  );
  return $element;
}