1

How hook_form_alter() works in Drupal? It will work when the form is just loaded right ? I need to modify the form variables before submission only . Suppose i am modifying some form variables after rendering the form and the submitting it , where i can write the code for this ?

Take an example as I have a form node_form() , and I am modifying the form variables in node_form_alter() and processing the submission in node_form_submit() function . Will node_form_alter work on pre submission too ? Or is there any workarounds ?

1 Answer 1

2

Yes, the code inside hook_form_alter() or hook_form_FORM_ID_alter() will be run when the $form array is being built, and before it is rendered.

All active modules that implement one of these two hooks will have their implementations called before the $form array is rendered. (They will be called in sequence according to their weights).

If you need to alter or manipulate the values being submitted by the form, you can use one of these two mentioned hooks to include in the $form array your custom #validate or #submit functions, where you can perform any kind of code processing you need.

A good source for finding examples of all these functionalities is the examples module, specifically around here.

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