You are here

function select2_select_element_process in Select 2 7

Select element process.

2 string references to 'select2_select_element_process'
select2_element_info in ./select2.module
Implements hook_element_info().
select2_element_info_alter in ./select2.module
Implements hook_element_info_alter().

File

./select2.module, line 253
Main file for Select2 module.

Code

function select2_select_element_process($element, &$form_state) {
  if (isset($element['#name']) && isset($form_state['select2_excluded']) && in_array($element['#name'], $form_state['select2_excluded'])) {
    $element['#select2_processed'] = TRUE;
    return $element;
  }
  $s2h_defaults = _select2_default_settings();
  if ($element['#type'] == 'select2_hidden') {
    if (!isset($element['#select2'])) {
      $element['#select2'] = $s2h_defaults;
    }
    else {
      $element['#select2'] = array_merge($s2h_defaults, $element['#select2']);
    }
    if ($element['#select2']['jqui_sortable']) {
      $element['#attached']['library'][] = array(
        'system',
        'ui.sortable',
      );
    }
  }
  if (isset($element['#select2_processed'])) {
    return $element;
  }
  if (variable_get('select2_use_for_all_select_elements', FALSE) && !isset($element['#select2'])) {
    if (!_select2_current_path_is_admin() || _select2_current_path_is_admin() && variable_get('select2_use_for_all_select_elements_for_admin_pages', FALSE)) {
      $element['#select2'] = $s2h_defaults;
    }
  }
  if (!isset($element['#select2'])) {
    $element['#select2_processed'] = TRUE;
    if (isset($element['#name'])) {
      $form_state['select2_excluded'][] = $element['#name'];
    }
    return $element;
  }
  if (isset($element['#options']) && is_array($element['#options'])) {
    foreach ($element['#options'] as $key => $value) {
      if (is_string($value)) {
        $element['#options'][$key] = htmlspecialchars_decode($value);
      }
    }
  }
  if (isset($element['#select2']['alter'])) {
    if (is_string($element['#select2']['alter'])) {
      if (function_exists($element['#select2']['alter'])) {
        $function = $element['#select2']['alter'];
        $function($element);
      }
    }
  }
  if ((!isset($element['#required']) || !$element['#required']) && !isset($element['#select2']['allowClear'])) {
    $element['#select2']['allowClear'] = TRUE;
  }
  if (!isset($element['#select2']['placeholder']) || !$element['#select2']['placeholder']) {
    if (isset($element['#empty_option'])) {
      $element['#select2']['placeholder'] = $element['#empty_option'];
    }
    elseif (isset($element['#options']['_none'])) {
      $element['#select2']['placeholder'] = $element['#options']['_none'];
      $element['#options'] = array(
        '' => $element['#options']['_none'],
      ) + $element['#options'];
      unset($element['#options']['_none']);
    }
  }
  $element['#attributes']['class'][] = 'use-select-2';
  $variant = variable_get('select2_compression_type', 'minified');
  $element['#attached']['libraries_load'][] = array(
    'select2',
    $variant,
  );
  $element_id = isset($element['#id']) ? $element['#id'] : (isset($element['#attributes']['id']) ? $element['#attributes']['id'] : drupal_clean_css_identifier($element['#name']));
  if ($element['#type'] == 'select2_hidden' && isset($element['#options']) && is_array($element['#options']) && (!isset($element['#validated']) || !$element['#validated'])) {
    $element['#attributes']['id'] = $element_id;
    $data = array();
    if (isset($element['#options'][''])) {
      unset($element['#options']['']);
    }
    foreach ($element['#options'] as $id => $value) {
      $data_element = new stdClass();
      $data_element->id = $id;
      $data_element->text = $value;
      if (isset($element['#select2']['taxonomy_vocabulary']) && $element['#select2']['taxonomy_vocabulary']) {
        if ($parents = taxonomy_get_parents_all($id)) {
          if (count($parents) > 1) {
            $parents = array_reverse($parents);
            $item_hover_title = '';
            foreach ($parents as $parent) {
              if ($parent->tid == $id) {
                continue;
              }
              $item_hover_title .= $parent->name . '->';
            }
            $item_hover_title .= $value;
            $data_element->hover_title = $item_hover_title;
          }
        }
      }
      $data[] = $data_element;
    }
    if (isset($item_hover_title)) {
      $element['#select2']['formatSelection'] = 'formatSelectionTaxonomyTermsItem';
      $element['#select2']['formatResult'] = 'formatResultTaxonomyTermsItem';
    }
    $element['#select2']['data'] = $data;
    unset($element['#options']);
  }
  if (isset($element['#field_name']) && $element['#field_name']) {
    $field_info = field_info_field($element['#field_name']);
    if ($field_info['cardinality'] != 1) {
      if ($element['#type'] != 'select') {
        $element['#select2']['multiple'] = TRUE;
      }
      $element['#select2']['jqui_sortable'] = TRUE;
      if ($field_info['cardinality'] > 1) {
        $element['#select2']['maximumSelectionSize'] = $field_info['cardinality'];
      }
    }
  }
  drupal_alter('select2_element', $element);
  _select2_set_element_settings($element_id, $element['#select2']);
  $element['#select2_processed'] = TRUE;
  return $element;
}