You are here

function autocomplete_deluxe_element_process in Autocomplete Deluxe 7

Same name and namespace in other branches
  1. 7.2 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 238
Define enhanced autocomplete wdiget.

Code

function autocomplete_deluxe_element_process($element) {
  $element['#attached'] = array(
    'library' => array(
      array(
        'system',
        'ui.autocomplete',
      ),
      array(
        'system',
        'ui.button',
      ),
    ),
    '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['#prefix'] = '<div class="clearfix autocomplete-container">';
  $element['#suffix'] = '</div>';
  $element['values'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'autocomplete-deluxe-values',
      ),
    ),
  );
  $element['textfield'] = array(
    '#type' => 'textfield',
    '#size' => isset($element['#size']) ? $element['#size'] : '',
    '#attributes' => array(
      'class' => array(
        'autocomplete-deluxe-form',
        'form-autocomplete',
      ),
      'id' => array(
        $html_id,
      ),
    ),
    '#default_value' => '',
  );
  $js_settings['autocomplete_deluxe'][$html_id] = array(
    'input_id' => $html_id,
    'min_length' => isset($element['#autocomplete_min_length']) ? $element['#autocomplete_min_length'] : 0,
    'multiple' => $element['#multiple'],
  );
  if (isset($element['#autocomplete_deluxe_path'])) {
    if ($element['#multiple']) {
      $element['value_field'] = array(
        '#type' => 'textfield',
        '#attributes' => array(
          'class' => array(
            'autocomplete-deluxe-value-field',
          ),
        ),
        '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
        '#prefix' => '<div class="autocomplete-deluxe-value-container">',
        '#suffix' => '</div>',
      );
      $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'],
    );
  }
  elseif (isset($element['#autocomplete_options'])) {
    $js_settings['autocomplete_deluxe'][$html_id] += array(
      'type' => 'list',
      'data' => $element['#autocomplete_options'],
    );
    $html_id_select = drupal_html_id('autocomplete-deluxe-input-select');
    $element['list_value'] = array(
      '#type' => 'select',
      '#options' => $element['#autocomplete_options'],
      '#multiple' => $element['#multiple'],
      '#attributes' => array(
        'class' => array(
          'autocomplete-deluxe-form',
          'form-autocomplete',
        ),
        'id' => array(
          $html_id_select,
        ),
      ),
      '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : '',
    );

    // Hide textfield so that, in case javascript is deactivated
    $element['textfield']['#attributes']['style'] = 'display: none;';
    $js_settings['autocomplete_deluxe'][$html_id]['select_input'] = $html_id_select;
  }
  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',
  );
  return $element;
}