I posted before about my first drupal module and our node hierarchy to node reference conversion project. This project is pretty much complete and will be getting rolled up to the live site shortly, I expect.
I said I would post some more details about this project. What I had in mind was talking about how I am using the Features module to package up many of the changes this conversion requires to make it easy to migrate from dev to staging to live.
Features is super cool and I highly recommend it. That being said, right now I’m a little more excited about a piece of this project that came together this week. Namely, my second drupal module—with considerably more than 13 lines of code—and specifically, its use of drupal’s ahah features.
I mentioned in the previous post using drupal’s php input format for quick and dirty coding. One thing I used this for was creating a booklist addition form for staff to add titles to booklists on our website. The workflow for a staff member using this system is:
- Logs into website.
- Finds title in library catalogue.
- Clicks a bookmarklet.
- Bookmarklet redirects to form which is prefilled with bibliographical info for the title.
- Chooses booklist they want to add title to from a pulldown menu.
- Submits form.
Since I was updating this form anyways for the node reference conversion, I figured I might as well do things right and convert it to a module. This turned out to be much easier than I imagined, the only real challenge being using drupal’s form api which I’m still getting comfortable with.
With the basic module out of the way, I decided to take a look at adding a feature to improve usability. The pulldown list of booklists on the form is quite long. And soon we will be adding kids and teen lists as well. So I thought a better workflow would be two pulldowns, the first to choose the list genre and the second to choose the list itself.
Of course, in this day and age, you don’t want to have to refresh the page just to create/update a form menu so I dug into the form api’s ahah capabilities.
I used this article and the sample module provided as a starting point for getting the dependent dropdowns working. The remaining problem was that whenever the selection on the first dropdown was changed, the form was getting submitted behind the scenes and new (and incorrect) records were being created.
I found the solution in this article and its suggestion to add the following line to my module’s ahah callback function:
$form_state['ahah_submission'] = TRUE;
and to then add some conditional logic to my submit function to check whether the form had been submitted by ahah or normally. Worked like a charm!
Here’s an animation of the module/ahah features in action (flash, opens in new window).