0
votes

I want to create an auto-complete form in my custom module that will be loaded in a block. Drupal doesn't seem to be loading the necessary Javascript libraries to work properly. How do I know what needs to be loaded and how/where do I tell Drupal to load these libraries?

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;
}

Form code:

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;   
}

The form loads, the field is there, but auto-complete isn't working :(

If I call the my-module-autocomplete path I do get a valid response back when compared with a Content Type edit form. The ajax spinner in the input field never appears so the ajax isn't being called. Realistically all I want is the autocomplete field...the submit will be handled manually.

3

3 Answers

0
votes

It's probably because you're reseting $form to an empty array at the beginning of the function. In Drupal 7 there's a bunch of stuff added to that element before it's passed through to your form function (that's why $form is passed to your function whereas in Drupal 6 it wasn't).

Just remove $form = array(); and it should work, other than that your code looks perfect.

0
votes

the following should work;

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
votes

this code is so pretty to add an auto-complete filed in block. But i just found a tiny notice here. if someone get an error

An ajax error occurred. http result code 200

then just add

exit();

after the line

drupal_json_output($matches);

hence fix the issue.