2012-01-19 2 views
0

블록에로드 될 내 맞춤 모듈에 자동 완성 양식을 만들고 싶습니다. 드루팔 (Drupal은 제대로 작동하기 위해 필요한 자바 스크립트 라이브러리를로드하지 않는 것 같습니다. 어떻게로드해야하는지, 어떻게/어디서 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 path 콘텐츠 유형 편집 양식과 비교할 때 유효한 응답을 다시 얻습니다. 입력 필드의 아약스 회 전자는 나타나지 않으므로 아약스가 호출되지 않습니다. 원하는 모든 것은 th입니다. e autocomplete field ... 제출이 수동으로 처리됩니다.

답변

0

아마도 이것은 기능 시작 부분에 $form을 빈 배열로 재설정했기 때문일 수 있습니다. 드루팔 (Drupal 7)에는 폼 함수 (form function)에 전달되기 전에 그 요소에 추가 된 많은 것들이 있습니다. (그래서 $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

이 코드는 자동 완료 블록을 추가하기에 너무 예쁩니다. 하지만 방금 작은 통지를 발견했습니다. 누군가 실수를하면

아약스 오류가 발생했습니다. 그럼 그냥 따라서 문제를 해결 라인

drupal_json_output($matches); 

exit(); 

를 추가

200 HTTP 결과 코드입니다.

관련 문제