Domanda

I am trying to have an autocomplete for a form I am using in drupal,

My current code looks like this:

function HOOK_form($form){
  $form['keyword'] = array(
'#type' => 'textfield',
'#attributes' => array(
        'title' => 'search field',
                    'label' => 'search field',
            ),
'#required' => TRUE,
'#autocomplete_path'=>'get_tax/autocomplete'
 );

 return $form;

}


 function HOOK_menu(){
   $menu = array(
       'get_tax/autocomplete/%' => array(
    'page callback' => 'tax_autocomplete_callback',
    'page arguments' => array(2),
    'type' => MENU_CALLBACK,
  ),
    );

   return $menu;
 }


 function tax_autocomplete_callback(){
         $terms = array();
         foreach(taxonomy_get_tree(5) as $tax){
             $terms[$tax -> tid] = check_plain($tax -> name);
        }
    drupal_json_output($terms);

 }

To me this should work, but it doesn't.

Any ideas?

È stato utile?

Soluzione

Inside your hook_menu implementation, try to get rid of % at the end of the path definition because it is not needed.

function HOOK_menu(){
   $menu = array(
       'get_tax/autocomplete' => array( // THE EDITED LINE.
       'page callback' => 'tax_autocomplete_callback',
       'page arguments' => array(2),
       'type' => MENU_CALLBACK,
       ),
    );

   return $menu;
 }

If it still does not work, kindly copy & paste the error message you see.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top