Posted by loze on August 8, 2008 at 3:29am
Jump to:
| Project: | Content Taxonomy |
| Version: | 6.x-1.0-beta1 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
is it possible to alter the option values for checkboxes before the form is rendered?
with hook_form_alter, I used devel's dsm() to print the $form object.
I can see my instance of a cck taxonomy field, but i can't see the options.
can i manipulate them somehow?
I would like to alter which terms display depending on the default value of another form field.
thanks
Comments
#1
Hi!
I would have expected the options being available in hook_form_alter() too...
I've to take a closer look at this.
#2
content taxonomy is using the option_widgets element of CCK. The elements are having an #process function to generate the options and other settings.
It seems like, that this #process function gets called after hook_form_alter().
At the moment I see two ways in altering the #options: either using #after_build or #pre_render.
This would look something like this:
function my_module_form_alter((&$form, $form_state, $form_id) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
$form['my_field']['#pre_render'][] = 'my_module_pre_render';
//or
$form['#after_build'][] = 'my_module_after_build';
}
}
function my_module_pre_render($element) {
$element['value']['#options'] = array('1' => 1, '2' => 2);
return $element;
}
function my_module_after_build($form, &$form_state) {
$form['my_field']['value']['#options'] = array(...);
return $form;
}
Instead of #after_build and #pre_render I should be possible to add an additional #process too, but I didn't try out.
I hope this information helps you. It seems to be a bit more complicated as in D5.
#3
Thank you, this helped greatly.
I ended up using #after_build
In doing this I also discovered that, for checkboxes, the values are in there twice (still not sure why, but select lists don't have this)
each option was listed as an array as $form['my_field']['value'][tid]
as well as key/values in $form['my_field']['value']['#options']
in my case i was simply removing some values based on another form value
so i had to loop through these and unset where appropriate.
it seems to work.
thanks!
#4
Automatically closed -- issue fixed for two weeks with no activity.