2011-07-21 17 views
2

我在表單alter中添加了一些自動完成功能。問題是,在回調中,只有文本字段中的字符串自動完成處於打開狀態,可用。我也想從回調中的另一個文本字段獲取值。這怎麼可能 ?Drupal自動完成,具有多個參數的回調

/** 
* Implements hook_form_alter(). 
*/ 
function webform_conversion_jquery_form_webform_client_form_1_alter(&$form, &$form_state, $form_id) { 
      //Load some extra function to process data 
     module_load_include('inc', 'webform_conversion_jquery', '/includes/dataqueries'); 

     //Add extra 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 a JSON object containing autocomplete suggestions for streets depending on the 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 as $street) { 
     $matches[$street->street] = $street->street; 
    } 
    drupal_json_output($matches); 
} 

我只是希望能夠在功能上張貼額外的信息:

conversion_street_autocomplete($street = '', $extraparameter)

回答

0

您可以通過覆蓋從autocomplete.js方法做到這一點在你自己的js中。下面是例子:

(function($) { 
    Drupal.behaviors.someModuleOverrideAC = { 
    attach: function(context, settings) { 
     // Next is copied and adjusted method from autocomplete.js 
     Drupal.jsAC.prototype.populatePopup = function() { 
     var $input = $(this.input); 
     var position = $input.position(); 
     // Show popup. 
     if (this.popup) { 
      $(this.popup).remove(); 
     } 
     this.selected = false; 
     this.popup = $('<div id="autocomplete"></div>')[0]; 
     this.popup.owner = this; 
     $(this.popup).css({ 
      top: parseInt(position.top + this.input.offsetHeight, 10) + 'px', 
      left: parseInt(position.left, 10) + 'px', 
      width: $input.innerWidth() + 'px', 
      display: 'none' 
     }); 
     $input.before(this.popup); 

     // Do search. 
     this.db.owner = this; 

     if ($input.attr('name') === 'field_appartment_complex') { 
      // Overriden search 
      // Build custom search string for apartments autocomplete 
      var $wrapper = $('div.apartments-autocomplete'); 
      var $elements = $('input, select', $wrapper); 
      var searchElements = {string: this.input.value}; 
      $elements.each(function() { 
      searchElements[$(this).data('address-part')] = $(this).val(); 
      }); 
      var string = encodeURIComponent(JSON.stringify(searchElements)); 
      this.db.search(string); 
     } 
     else { 
      // Default search 
      this.db.search(this.input.value); 
     } 
     }; 
    } 
    }; 
}(jQuery)); 

在您的服務器的回調:

function some_module_autocomplete_ajax($string) { 
    // Decode custom string obtained using overriden autocomplete js. 
    $components = drupal_json_decode(rawurldecode($string)); 
    // Do you search here using multiple params from $components 
} 
2

我有同樣的問題,並想出一個辦法,這是不是太費勁。它涉及覆蓋文本字段主題,然後將您的參數傳遞給主題函數。

首先創建申報您的主題功能:

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

接下來,我們需要的主題和變量添加到我們的表單元素。在我的情況下,表單元素是字段控件的一部分:

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', 
     // THIS IS THE IMPORTANT PART - ADD THE THEME AND THE VARIABLE. 
     '#theme' => 'my_module_autocomplete', 
     '#my_module_variable' => $field['field_name'], 
    ); 
    } 
    return $element; 
} 

然後執行主題函數。這是includes/form.inctheme_textfield副本有一個重要區別 - 我們的變量追加到自動完成路徑:

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'; 
    // THIS IS THE IMPORTANT PART. APPEND YOUR VARIABLE TO THE 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; 
} 

現在變量將作爲對自動完成回調函數的第一個參數:

function _my_module_autocomplete($my_module_variable, $search_string) { 
    // Happy days, we now have access to our parameter. 
} 
0

爲了防止任何人遇到此問題,我在找出一個很好的解決方案,同時試圖找出如何做到這一點。我有一年的選擇列表,並決定了什麼數據顯示在自動填充字段。該解決方案基本上有一個選擇列表的ajax回調函數,然後可以使用url中的額外參數更新自動填充字段。無論如何,它在下面的文章中得到了很好的解釋。

http://complexdan.com/passing-custom-arguments-drupal-7-autocomplete/

*一個值得注意的,我快要瘋了試圖找出它爲什麼沒有工作,事實證明你不能在頁面上相同的形式兩次(我需要,因爲我對移動設備的顯示方式不同),因爲您使用的是ajax回調標識。我添加了一個額外的參數來實現這一點。在下面的例子中它被稱爲uniqueid。

function report_cards_comparison_form($form, &$form_state, $uniqueid) { 

    $curryear = t('2012'); 

    $form['year_select'] = array(
     '#title' => t('School Year'), 
     '#type' => 'select', 
     '#options' => array(
      '2012' => t('2012'), 
      '2013' => t('2013'), 
      '2014' => t('2014'), 
      '2015' => t('2015'), 
     ), 
     '#default_value' => $curryear, 
     '#ajax' => array(
      'callback' => 'report_cards_comparison_form_callback', 
      'wrapper' => $uniqueid, 
      'progress' => array(
       'message' => 'Updating Schools...', 
       'type' => 'throbber' 
      ), 
     ), 

); 

    $form['choice'] = array(
    //'#title' => t('Search By: School Name'), 
    '#type' => 'textfield', 
    '#attributes' => array(
     'class' => array('school-choice'), 
     'placeholder' => t('Start Typing School Name...'), 
    ), 
    '#required' => TRUE, 
    '#autocomplete_path' => 'reportcards/autocomplete/' . $curryear, 
    '#prefix' => '<div id="' . $uniqueid . '">', 
    '#suffix' => '</div>', 
); 

    $form['submit'] = array(
    '#type' => 'submit', 
    '#prefix' => '<div class="submit-btn-wrap">', 
    '#suffix' => '</div>', 
    '#value' => t('Search'), 
    '#attributes' => array('id' => 'add-school-submit'), 
); 

    return $form; 
} 

/** 
* Ajax Callback that updates the autocomplete ajax when there is a change in the Year Select List 
*/ 
function report_cards_comparison_form_callback($form, &$form_state) { 
    unset($form_state['input']['choice'], $form_state['values']['choice']); 
    $curryear = $form_state['values']['year_select']; 

    $form_state['input']['choice'] = ''; 
    $form['choice']['#value'] = ''; 
    $form['choice']['#autocomplete_path'] = 'reportcards/autocomplete/' . $curryear; 

    return form_builder($form['#id'], $form['choice'], $form_state); 
} 

我可以通過這樣做來調用窗體...

print render(drupal_get_form('report_cards_comparison_form', 'desktop-schoolmatches'));