How can I add an object to a form array to make it available to make it available to the hook_form_alter() implementations?

I have an object I pass to a form, which may have new property. I would like to implement hook_form_alter() in another module to add/change form items based on that property.

There is no need for the item to be available after submission. I would like to avoid the overhead of using a value form element.

1 Answer 1


You don't have to add the entire object to the $form array: You can just add the property you need to share with the hook_form_alter() implementations, for example with the following code.

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

You don't have to worry about adding overhead because the values submitted in a form are transient by design. This means that, if none of the submission handlers store the submitted values, they will be lost anyway.

  • As additional note, $form['#mymodule_new'] is available to the form validation handlers, and the form submission handlers. The value used as index should not be one already used by other Drupal modules, including the Drupal core modules.
    – apaderno
    Mar 4, 2011 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.