Add new comment

New module: Hierarchical Select

published on October 27, 2007

Almost 3 weeks two months ago now, I have released the Hierarchical Select module. (This post was originally written during the DrupalCon, but time has been sparse since then and bugs were there, which is why I’m posting this so late.) It defines a new form element: hierarchical_select. It makes the selection of something in a hierarchy much more usable. By default, selects of the taxonomy module (on node forms) are forced to use hierarchical selects - if the vocabulary is hierarchical. The same is applied to content_taxonomy CCK fields, when the select widget is used.
Note that the hierarchical select form element does not support the selection of multiple items. And obviously, it only selects the “deepest” item.
I’ll immediately skip to the demo, because a live demo says so much more than words.

And it has an API!

This part is only interesting to people who want to make use of this form element for other modules than taxonomy.

The module comes with an API, so that any module that manages some sort of hierarchy, can define a rendering function that will be used to render the hierarchical selects. It can’t be done completely automagically, because it varies greatly from case to case what input data is necessary to generate the necessary tree structure.

For example: taxonomy needs a vocabulary id and the id of the currently selected term (term id). Another module may only require one id, while another may require 3 ids. You will always get the id of the newly selected item in any of the selects back (which is the term id in the case of taxonomy).

hook_hierarchical_select_render($hsid, $selection, $params)
  • $hsid: hierarchical select id, will just be used as a unique identifier in the HTML
  • $selection: the value of the item that was selected by the user
  • $params: an array of parameters that are passed around, these are considered “static”, e.g. the vocabulary id

You can look at modules/taxonomy.inc for an example implementation. See API.txt for details.

Future: Drupal 6 and CCK 2

There’s room for improvements in the API. But that’ll be for a second version, which will be Drupal 6 only, because of the interesting change in CCK 2: CCK widgets will simply be FAPI form elements. That means that we’ll be able to use the hierarchical select form element for several CCK fields!