I want to alter the form for a content type I created for videos. The type is called Videos. How would I use hook_form_alter to alter the form to create the content?
4 Answers
The form ID is used as the id attribute on the form when it renders.
For content types the pattern is also predictable: node-[TYPE_NAME]-form. So in your case you're looking for: hook_form_node_videos_form_alter().
Another way to handle it is to use the main hook_form_alter() and check the $form_id parameter when you load the form you'd like to target.
- i get syntax errors using - between node and videos and when i use _ it doesnt seem to be executing. Can i simply add this hook to the video module or do i need my own module just to alter the video form? so my function looks like video_form_node_video_form_alterBrandenB171– BrandenB1712016-05-06 20:56:27 +00:00Commented May 6, 2016 at 20:56
- 1Sorry I mistyped that - they should always be underscores. Do NOT update the existing module as future updates to that module will overwrite your changes. Create your own module.acrosman– acrosman2016-05-06 21:03:46 +00:00Commented May 6, 2016 at 21:03
- perfect that worked. is that the same form for editing the node?BrandenB171– BrandenB1712016-05-06 21:17:46 +00:00Commented May 6, 2016 at 21:17
- 1Yes, nodes have just one form used for both create and edit.acrosman– acrosman2016-05-06 21:19:25 +00:00Commented May 6, 2016 at 21:19
You can also just look at the generated markup. the form_id is always present as a hidden form_element, so just search for "form_id" in the source code and you should find it. Make sure you're looking at the correct form wrapper, often there are multiple forms (e.g. search/signup/login blocks)
Enable the development module then in your own .module file to get the form_id
function hook_form_alter(&$form, &$form_state, $form_id) { dpm(form_id);//get the form_id dpm(form);//get the form }
Save the file flush the cache and you can see the form_id.
In Drupal 8, navigate to the devel tab on the webform, click on render. Expand the variables and search for 'webform_id'. You can find the webform id here.