List Tree
Categories
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Downloads
Component created
Component changed
Component body
The list field type already comes with a widget for check boxes and radio buttons, but they are rendered in a single, non-hierarchical list. What if you have a more complex structure to your list options? What if you'd like to actually have the options arranged in a hierarchy? Well, that's where this module comes in to play.
Right now the tree is rendered only when the options are generated by a function. So, for this module to work, you also need the Content Construction Kit (CCK) module, which creates fields on list widget edit forms to be able to specify php code to generate the options, or to specify a function in a module you write. The module doesn't depend on CCK, but without CCK, you won't be able to specify your own function. When specifying the function, use the function name option, not the php code snippet to call your function.
Your function should return a standard, options-group-constructed array or a flat array, depending on the last parameter to the function as shown below:
function my_module_tree_options($field = NULL, $instance = NULL, $entity_type = NULL, $entity = NULL, $cacheable = NULL, $flat = TRUE)
$options = array(
'option_1_key' => 'The display text for option 1',
'option_2_key' => 'The display text for option 2',
'Option Group 1 Display Title' => array(
'option_3_key' => 'The display text for option 3',
'option_4_key' => 'The display text for option 4',
),
);
// Or whatever logic you want to use to create the nested array.
// MAKE SURE to include the following lines to flatten your array for
// use by the list field module, which needs it flat.
if ( $flat ) {
$options = options_array_flatten($options);
}
return $options;
}
The last parameter is important as is flattening the array, because the list field module will need your options flattened (and will call your function without the last parameter), but this module also needs them in the hierarchy, and will call it with the last parameter set to FALSE.
This module will render a widget that has check boxes or radio buttons (depending on the cardinality) in a hierarchy of unordered lists.
Credit where credit is due, I pinched most of code for this module from Taxonomy Term Reference Tree Widget, so many thanks to its maintainers!
