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

I have an object I pass to a form, which may have the 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, if they are not stored in the database.

As additional note, $form['#mymodule_new'] is available to form validation and submission handlers. The array key should not be already used by other Drupal modules, including Drupal core modules, to avoid conflicts.


Your Answer

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

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