Wednesday, April 13, 2011

jQuery Magic with hook_form_alter in Drupal

There isn't much tutorial in the Pro Drupal book about ahah, but I'm trying to start off with something simple. I have two content types, 'topic' and 'tips' and categories. The topics are a node reference field in tips. The topics are organized by category.

When filling out the form for a tip, I would like the category the topic belongs to appear in a div when select list of topics is chosen.

I found a jQuery tutorial, but curious how to use ahah in all of this.

From stackoverflow
  • AHAH is used to replace a portion of the HTML on your page. So you could, in a module, create a menu item that points to a function that would discover and return the text you want in the div.

    This is shown in Pro Drupal Development on page 270. You can download the poof module from

    http://www.drupalbook.com/files/PDD2ed_code.zip

    and inspect it to see how it works. The only difference is that the poof module uses a submit form element instead of the select form element that you want to use, and it returns the hardcoded string 'POOF' whereas you want to discover and return the name of the category. But it's a good starting point.

  • AHAH forms in Drupal are not yet very easy. In Drupal 5 it's near impossible, in Drupal 6 it's very doable but the code is very ugly and it looks like it will be improved a lot in Drupal 7.

    If you're using Drupal 6, I'd recommend trying the AHAH Helper module a try. It allows you to have AHAH forms and not write any JavaScript code.

0 comments:

Post a Comment