0

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.

1
  • Does it have something to do with loading the form in a block? Commented Jan 19, 2012 at 18:44

2 Answers 2

2

See the examples module for a straight forward AJAX Example.

1
  • I looked in the code, but didn't see any drupal_add_x calls or any includes...I assume Drupal would add the necessary libraries while it was building the blocks/pages?. Commented Jan 19, 2012 at 18:23
1

Genuinely non-trivial, so here are some links:

Drupal AJAX API. Alternately, drupal_add_js()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.