You are here

function autocomplete_deluxe_element_process in Autocomplete Deluxe 7.2

Same name and namespace in other branches
  1. 7 autocomplete_deluxe.module \autocomplete_deluxe_element_process()

Generates the basic form elements and javascript settings.

1 string reference to 'autocomplete_deluxe_element_process'
autocomplete_deluxe_element_info in ./autocomplete_deluxe.module
Implements hook_element_info().

File

./autocomplete_deluxe.module, line 308
Define enhanced autocomplete wdiget.

Code

function autocomplete_deluxe_element_process($element) {
  $element['#attached'] = array(
    'library' => array(
      array(
        'system',
        'ui.autocomplete',
      ),
      array(
        'system',
        'ui.button',
      ),
      array(
        'system',
        'ui.sortable',
      ),
    ),
    'js' => array(
      drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.js',
    ),
    'css' => array(
      drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.css',
    ),
  );

  // Workaround for problems with jquery css in seven theme.
  if ($GLOBALS['theme'] == 'seven') {
    $element['#attached']['css'][] = drupal_get_path('module', 'autocomplete_deluxe') . '/autocomplete_deluxe.seven.css';
  }
  $html_id = drupal_html_id('autocomplete-deluxe-input');
  $element['#after_build'][] = 'autocomplete_deluxe_after_build';

  // Set default options for multiple values.
  $element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;
  $element['textfield'] = array(
    '#type' => 'textfield',
    '#size' => isset($element['#size']) ? $element['#size'] : '',
    '#attributes' => array_merge_recursive($element['#attributes'], array(
      'class' => array(
        'autocomplete-deluxe-form',
      ),
      'id' => array(
        $html_id,
      ),
      'aria-label' => t('Autocomplete'),
    )),
    '#default_value' => '',
    '#prefix' => '<div class="autocomplete-deluxe-container">',
    '#suffix' => '</div>',
  );
  $js_settings['autocomplete_deluxe'][$html_id] = array(
    'input_id' => $html_id,
    'multiple' => $element['#multiple'],
    'required' => $element['#required'],
    'limit' => isset($element['#limit']) ? $element['#limit'] : 10,
    'min_length' => isset($element['#min_length']) ? $element['#min_length'] : 0,
    'use_synonyms' => isset($element['#use_synonyms']) ? $element['#use_synonyms'] : 0,
    'delimiter' => isset($element['#delimiter']) ? $element['#delimiter'] : '',
    'not_found_message' => isset($element['#not_found_message']) ? $element['#not_found_message'] : t("The term '@term' will be added."),
  );
  if (isset($element['#autocomplete_deluxe_path'])) {
    if (isset($element['#default_value'])) {

      // Split on the comma only if that comma has zero, or an even number of
      // quotes in ahead of it.
      // http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes
      $default_value = preg_replace('/,(?=([^\\"]*\\"[^\\"]*\\")*[^\\"]*$)/i', '"" ""', $element['#default_value']);
      $default_value = '""' . $default_value . '""';
    }
    else {
      $default_value = '';
    }
    if ($element['#multiple']) {
      $element['value_field'] = array(
        '#type' => 'textfield',
        '#attributes' => array(
          'class' => array(
            'autocomplete-deluxe-value-field',
          ),
          'aria-label' => t('Autocomplete Value'),
        ),
        '#default_value' => $default_value,
        '#prefix' => '<div class="autocomplete-deluxe-value-container">',
        '#suffix' => '</div>',
        '#required' => isset($element['#required']) ? $element['#required'] : FALSE,
        '#element_validate' => array(
          'autocomplete_deluxe_validate_multiple',
        ),
        '#parent_element' => &$element,
      );
      $element['textfield']['#attributes']['style'] = array(
        'display:none',
      );
    }
    else {
      $element['textfield']['#default_value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
    }
    $js_settings['autocomplete_deluxe'][$html_id] += array(
      'type' => 'ajax',
      'uri' => $element['#autocomplete_deluxe_path'],
    );
  }
  else {

    // If there is no source (path or data), we don't want to add the js
    // settings and so the functions will be abborted.
    return $element;
  }
  $element['#attached']['js'][] = array(
    'data' => $js_settings,
    'type' => 'setting',
  );
  $element['#tree'] = TRUE;
  return $element;
}