0Day Forums
drupal form alter in webform forms - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: CMS (https://0day.red/Forum-CMS)
+---- Forum: Drupal (https://0day.red/Forum-Drupal)
+---- Thread: drupal form alter in webform forms (/Thread-drupal-form-alter-in-webform-forms)



drupal form alter in webform forms - Promeleager854 - 07-27-2023

I know that there is possible to use some functions to alter drupal core forms: `hook_form_alter()`.
Can we use this with Drupal forms that are created with the Webform module?


RE: drupal form alter in webform forms - organzine70539 - 07-27-2023

I am not quite sure what you are trying to do, but since webform module creates a content type - webform - you can alter forms created by webform purely through the admin interface - add new inputs and input types, specify whether they are required or not etc.

for example, a "contact us" form can have whatever inputs you want - unlike the core Drupal contact form which IIRC only has an email address and a textarea.


RE: drupal form alter in webform forms - guylaineqraxd - 07-27-2023

Yes, if for some reason you need to make a change to the webform which you can't do by editing the webform node, then you can use hook_form_alter to change the form as well, as the webform is created by the form api.

That said, do poke around in some of the corners of webform - it comes with a number of options for dynamically filling or changing parts of the form already.


RE: drupal form alter in webform forms - manimozhispqknphuxl - 07-27-2023

You can do it,

you just need the id of the node and then use the id like in
hook_form_<FORMID>_alter()

the FORMID generated is webform_client_form_<NODEID>

where NODEID is the id of the node

so if you have a module named mymodule and a node with id 44 which has a webform

function mymodule_form_webform_client_form_44_alter(&$form, &$form_state) {
// code here;
}


RE: drupal form alter in webform forms - unforestallable461136 - 07-27-2023

You can use `hook_form_alter()`, accessing elements via `$form['submitted']`.


RE: drupal form alter in webform forms - bickerers519936 - 07-27-2023

In Drupal 7, you can either use `hook_form_alter()` or `hook_form_<formid>_alter()`, which ever you prefer. Just make sure you get the naming and parameters right. Drupal 6 only supports `hook_form_alter()` however.

When you create these functions, also remember that Drupal may not always pick up on them until you've flushed the cache.

Another important thing to note is that if you want to make changes to the webform fields, you have to make changes in `$form['submitted']`. I made the mistake of originally trying to edit `$form['#node']->webform['components']`, which has no effect.

More information can be found here:

[To see links please register here]


Hope that can help.