2012-01-19 59 views
0

我想在自定義模塊中創建一個自動完成的表單,該表單將被加載到一個塊中。 Drupal似乎沒有加載必要的Javascript庫才能正常工作。我如何知道需要加載哪些內容以及如何告知Drupal加載這些庫?我可以在塊中創建Drupal自動完成文本字段嗎?

hook_block_view:

function my_module_block_view($delta = '') { 
    //The $delta parameter tells us which block is being reqested. 
    switch ($delta) { 
     case 'my_module_my_block': 
      $block['subject'] = t('Block Subject'); 
      $block['content'] = drupal_get_form('my_module_my_form'); 
      break; 
    } 

    return $block; 
} 

表單代碼:

function my_module_my_form($form, &$form_state) { 
    $form = array(); 

    $form['term'] = array(
     '#type' => 'textfield', 
     '#autocomplete_path' => 'my-module-autocomplete' 
    ); 

    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Add', 
); 

    return $form; 
} 

的形式加載,本場是存在的,但自動完成不工作:(

如果我叫my-module-autocomplete路徑與Content Type編輯表單相比,我確實得到了有效的響應。輸入字段中的ajax spinner永遠不會出現,因此ajax沒有被調用,實際上所有我想要的是th e自動填充字段...提交將被手動處理。

回答

0

這可能是因爲您正在將$form重置爲函數開頭處的空數組。在Drupal 7中,在將元素傳遞到窗體函數之前,會添加一些東西(這就是爲什麼要將$form傳遞給您的函數,而在Drupal 6中則不是)。

只要刪除$form = array();它應該工作,除了你的代碼看起來很完美。

+0

試過...仍然沒有運氣。它好像有一些東西我應該運行或告訴Drupal,以便它知道我想在我的表單上使用Drupal AJAX ... – SomethingOn

+0

您是否有模塊或主題覆蓋'theme_textfield'?這就是JS庫被添加到自動完成的地方 – Clive

0

以下應該工作;

function mymodule_block_info() { 
    $blocks['mymodule'] = array(
    // The name that will appear in the block list. 
    'info' => t('My Module'), 
    // Default setting. 
    'cache' => DRUPAL_NO_CACHE, 
); 
    return $blocks; 
} 

function mymodule_block_view($delta = ''){ 
    switch($delta){ 
    case 'mymodule': 
     if(user_access('access content')){ //good idea to check user perms here 
     $block['subject'] = t('My Module'); 
     $block['content'] = 'Hi :)'; 
     $block['content'] = drupal_get_form('mymodule_form'); 
     return $block; 
     } 
     break; 
    } 
} 

function mydmodule_menu() { 
    $items['module/autocomplete'] = array(
    'page callback' => 'mymodule_autocomplete', 
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK 
); 
    return $items; 
} 

function mymodule_form($form, &$form_state) { 
    $form['greenentry'] = array(
      '#type' => 'textfield', 
      '#title' => t('Enter'), 
      '#autocomplete_path' => 'mymodule/autocomplete', 
    ); 


    $form['submit'] = array(
      '#type' => 'submit', 
      '#value' => t('Submit'), 
    ); 
    return $form; 
} 

function mymodule_autocomplete($string) { 
    $matches = array(); 

    // Some fantasy DB table which holds cities 
    $query = db_select('cities', 'c'); 

    // Select rows that match the string 
    $return = $query 
    ->fields('c', array('city')) 
    ->condition('c.city', '%' . db_like($string) . '%', 'LIKE') 
    ->range(0, 10) 
    ->execute(); 

    // add matches to $matches 
    foreach ($return as $row) { 
    $matches[$row->url] = check_plain($row->url); 
    } 

    // return for JS 
    drupal_json_output($matches); 
} 
0

這段代碼非常漂亮,可以在塊中添加一個自動完成的字段。但我在這裏發現了一個小通知。如果有人得到一個錯誤

發生ajax錯誤。 HTTP結果代碼200

然後只需添加

exit(); 

drupal_json_output($matches); 

之後,因此解決該問題。

相關問題