You are here

function select2_process_textfield in Select 2 7

Process textfield.

1 string reference to 'select2_process_textfield'
select2_element_info_alter in ./select2.module
Implements hook_element_info_alter().

File

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

Code

function select2_process_textfield($element, &$form_state) {
  if (isset($element['#field_name']) && $element['#field_name']) {
    $field_info = field_info_field($element['#field_name']);
  }
  if (!variable_get('select2_use_for_ac_elements', FALSE) || !isset($element['#autocomplete_path']) || !$element['#autocomplete_path'] || isset($element['#select_2_processed']) && $element['#select_2_processed']) {

    // Must check form field for entity field setting.
    if (isset($field_info) && (isset($element['#select2']) && isset($element['#select2']['use_select2']) && $element['#select2']['use_select2'])) {
    }
    else {
      return $element;
    }
  }
  $element_id = isset($element['#id']) ? $element['#id'] : (isset($element['#attributes']['id']) ? $element['#attributes']['id'] : drupal_clean_css_identifier($element['#name']));
  if ((!isset($element['#field_info']) || !$element['#field_info']) && isset($element['#field_name']) && $element['#field_name']) {
    $field_info = field_info_field($element['#field_name']);
    $element['#field_info'] = $field_info;
  }
  $element['#attributes']['class'][] = 'use-select-2';
  if (!isset($element['#ajax_processed']) || !$element['#ajax_processed']) {
    $element['#ajax_processed'] = TRUE;
  }
  if (!isset($element['#select2'])) {
    $element['#select2'] = array();
  }
  $s2_ac_defaults = array(
    'autocomplete_path' => $element['#autocomplete_path'],
    'minimumInputLength' => 1,
    'formatResult' => 'acFormatResult',
    'formatSelection' => 'acFielsFormatSelection',
    'initSelection' => 'acS2InitSelecttion',
    'ajax' => 'getAjaxObjectForAcElement',
    'containerCssClass' => 'ac-replaced-by-select2',
  );
  $element['#attributes'] += array(
    'data-select2-ajax' => 'ac_element_ajax',
    'data-select2-ajaxpath' => url($element['#autocomplete_path']),
    'data-select2-initSelection' => 'acS2InitSelecttion',
  );
  if (!isset($element['#required']) || !$element['#required']) {
    $s2_ac_defaults['allowClear'] = TRUE;
    $s2_ac_defaults['placeholder'] = t('Start search');
  }
  if (isset($field_info) && $field_info['type'] == 'taxonomy_term_reference') {

    // Need to add custom properties for taxonomy term reference field.
    $s2_ac_defaults['taxonomy_ref_ac_allowed'] = TRUE;
    if ($field_info['cardinality'] != 1) {
      $s2_ac_defaults['tokenSeparators'] = array(
        ',',
      );
      $s2_ac_defaults['separator'] = ',';
      $s2_ac_defaults['comma_replacement'] = SELECT2_COMMA_REPLACEMENT;
      $s2_ac_defaults['initSelection'] = 'taxonomyTermRefAcS2InitSelecttion';
      $element['#attributes']['data-select2-initSelection'] = 'taxonomyTermRefAcS2InitSelecttion';
      $description_prefix = '';
      if (isset($element['#description']) && $element['#description']) {
        $description_prefix = '<br />';
      }
      else {
        $element['#description'] = '';
      }
      $element['#description'] .= $description_prefix . t('For change order of terms, you can drag and drop them into desired position.') . '<br />' . t('Symbol &quot;,&quot; will aumaticaly separete the inputted terms.');
    }
  }
  if (isset($element['#entity_type']) && isset($element['#field_name']) && isset($element['#bundle'])) {
    if (!isset($element['#field_instance']) || !$element['#field_instance']) {
      $field_instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
      $element['#field_instance'] = $field_instance;
    }
    else {
      $field_instance = $element['#field_instance'];
    }
    if (strstr($field_instance['widget']['type'], '_tags') !== FALSE) {
      $s2_ac_defaults['separator'] = ',';
      $s2_ac_defaults['tokenSeparators'] = array(
        ',',
      );
      $s2_ac_defaults['initSelection'] = 'taxonomyTermRefAcS2InitSelecttion';
      if ($element['#value']) {
        $s2_ac_defaults['comma_replacement'] = SELECT2_COMMA_REPLACEMENT;
      }
    }
  }
  if (isset($field_info) && $field_info['cardinality'] != 1) {
    $s2_ac_defaults['multiple'] = TRUE;
    $s2_ac_defaults['jqui_sortable'] = TRUE;
    if ($field_info['cardinality'] > 1) {
      $s2_ac_defaults['maximumSelectionSize'] = $field_info['cardinality'];
    }
  }
  $result_settings = isset($element['#select2_defaults']) ? array_merge($s2_ac_defaults, $element['#select2'], $element['#select2_defaults']) : array_merge($s2_ac_defaults, $element['#select2']);
  $element['#select2'] = $result_settings;
  $variant = variable_get('select2_compression_type', 'minified');
  $element['#attached']['libraries_load'][] = array(
    'select2',
    $variant,
  );
  $element['#select_2_processed'] = TRUE;
  if (isset($element['#select2']['jqui_sortable']) && $element['#select2']['jqui_sortable']) {
    $element['#attached']['library'][] = array(
      'system',
      'ui.sortable',
    );
  }
  if (isset($element['#select2']['allowClear']) && $element['#select2']['allowClear'] && (!isset($element['#attributes']['placeholder']) && (!isset($element['#select2']['placeholder']) || !$element['#select2']['placeholder']))) {
    $element['#select2']['placeholder'] = t('Type text for search');
  }
  drupal_alter('select2_element', $element);
  _select2_set_element_settings($element_id, $element['#select2']);
  $element['#autocomplete_path'] = FALSE;
  if (isset($element['#select2']['comma_replacement'])) {
    if (!isset($element['#element_validate'])) {
      $element['#element_validate'] = array();
    }
    array_unshift($element['#element_validate'], '_select2_comma_replacement_validate');
  }
  return $element;
}