Drupal autocomplete, callback with multiple parameters -


i adding autocomplete on form alter. problem in callback, string in textfield autocomplete on, available. want access value textfield in callback. how possible ?

/**  * implements hook_form_alter().  */ function webform_conversion_jquery_form_webform_client_form_1_alter(&$form, &$form_state, $form_id) {           //load function process data         module_load_include('inc', 'webform_conversion_jquery', '/includes/dataqueries');          //add js files         drupal_add_js(drupal_get_path('module', 'webform_conversion_jquery') . '/js/conversionform.js');         $form['submitted']['correspondentadress']['cor_street']['#autocomplete_path'] = 'conversionform/conversion_street';     } }  /**  * implements hook_menu().  */ function webform_conversion_jquery_menu() {     $items = array();       $items['conversionform/conversion_street'] = array(         'title' => 'conversion street autocomplete',         'page callback' => 'conversion_street_autocomplete',         'access callback' => 'user_access',         'access arguments' => array('access content'),         'type' => menu_callback,     );      return $items; }  /**  * retrieve json object containing autocomplete suggestions streets depending on zipcode.  */ function conversion_street_autocomplete($street = '') {     $street = "%" . $street . "%";     $matches = array();     $result = db_select('conversion_adresslist')                     ->fields('conversion_adresslist', array('street'))                     ->condition('street', $street, 'like')                     ->execute();     foreach ($result $street) {         $matches[$street->street] = $street->street;     }     drupal_json_output($matches); } 

i want able post information in function:

conversion_street_autocomplete($street = '', $extraparameter)

i had same problem , have figured out way, not strenuous. involves overriding textfield theme , passing parameter theme function.

first create declare theme function:

function mymodule_theme() {   $theme_hooks = array(     'my_module_autocomplete' => array(       'render element' => 'element',     ),   );   return $theme_hooks; } 

next need add theme , variable our form element. in case, form element part of field widget:

 function my_module_field_widget_form($form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {    if($instance['widget']['type'] == 'my_module_field_type') {      $element['my_module_field'] = array(       '#type' => 'textfield',       '#autocomplete_path' => 'my-module/autocomplete',       // important part - add theme , variable.       '#theme' => 'my_module_autocomplete',       '#my_module_variable' => $field['field_name'],     );   }   return $element; } 

then implement theme function. copy of theme_textfield includes/form.inc 1 important difference - append variable autocomplete path:

function theme_my_module_autocomplet($variables) {   $element = $variables['element'];   $element['#attributes']['type'] = 'text';   element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));   _form_set_class($element, array('form-text'));    $extra = '';   if ($element['#autocomplete_path'] &&     drupal_valid_path($element['#autocomplete_path'])) {     drupal_add_library('system', 'drupal.autocomplete');     $element['#attributes']['class'][] = 'form-autocomplete';      $attributes = array();     $attributes['type'] = 'hidden';     $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';     // important part. append variable autocomplete path.     $attributes['value'] = url($element['#autocomplete_path'] . '/' . $element['#my_module_variable'], array('absolute' => true));     $attributes['disabled'] = 'disabled';     $attributes['class'][] = 'autocomplete';     $extra = '<input' . drupal_attributes($attributes) . ' />';   }    $output = '<input' . drupal_attributes($element['#attributes']) . ' />';    return $output . $extra; } 

now variable available first parameter on autocomplete callback function:

function _my_module_autocomplete($my_module_variable, $search_string) {   // happy days, have access our parameter. } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -