Question

What is the best way to add an object to a form array so that any form_alter hook can interrogate the form for the object and act accordingly.

For example, I have an object I pass to my form which may have the property "new" added to. I would like then to use the hook_form_alter from another module to add/change form items based on the property new.

There is no need for the item to be available after submission in this scenario, so I wanted to avoid the overhead of using #type = 'value' add adding the object as a form item.

Was it helpful?

Solution

You don't have to add the entire object as a value to the $form array: you could just add the applicable property. So add something like:

$form['#mymodule_new'] = $object->new;

And then query against $form['#mymodule_new'] in your hook_form_alter() implementation.

You don't have to worry about additional overhead: form values are transient by design. If you don't do anything with the value during your submit handler, it will be lost anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top