How do I get the form id of my form? I've just begun to make my modules and I'm at a standstill. I got this code from someone else and am trying to customize it:

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the form id for the node form') {
    $form['#submit'][] = 'my_custom_submit_handler';

9 Answers 9


Try printing the form variables with PHP function print_r.

function hook_form_alter(&$form, &$form_state, $form_id) {

When you visit the page the form is on, the variables (and name) will be displayed.

An easier way of accessing (or rather viewing) the information would be using dpm() function provided by the Devel module. You can use it in the same way you would use print_r.

  • 1
    Given the reported code, there is no need to see the content of the $form variable.
    – apaderno
    Jun 26, 2011 at 13:52
  • 12
    Also $form_id is a param you can directly print it !!
    – niksmac
    Apr 16, 2012 at 11:19
  • 2
    If you have Devel enabled, just dpm($form_id);
    – No Sssweat
    Jun 7, 2017 at 7:43

Finding FORM ID without installing module

Finding the Form ID is very easy no need to install modules and all

STEP 1: Open you from go to its edit page(OR inspect your form).

STEP 2: if its a node form find "node-form" in the inspect element

Finding the form id check you are in the form element.

STEP 3: see attached image the highlighted green text is the ID of the form

STEP 4: IMPORTANT Finally if you want to use it in hook_form_alter() replace the hypen with underscore

For example: yourform_id_with_content_type_name_form

Like wise you can find id of any form in drupal all you have to do is inspect->find form element and search for the ID attribute and use it

Hope it helps :)

  • 1
    This didn't work for me when I wanted to protect the comment form in a Drupal 8 site. I found a solution in this comment on drupal.org, where it says: 1) Find a hidden input element whose name attribute has value "form_id". 2) Look at the input element's value attribute - the attribute's value is the desired form ID. Example: The comment form on story nodes has the form ID "comment_comment_node_story_form". If you like you can incorporate this new information in your answer.
    – herzbube
    Sep 26, 2018 at 20:58
  • sometime the id changes depending on the context. niksmak suggestion is a better approach
    – Daniel
    Oct 15, 2021 at 11:22

If your webform is located at "www.mydomain.com/node/351" then your form ID will be "webform_client_form_351". So, whatever your nid is - that's your form ID.

  • 2
    The question is not about the Webform module. It is about the node edit form.
    – apaderno
    Sep 3, 2012 at 2:36
  • The question is about the node edit form but if you want to find any webform form ID this works correcty.
    – herci
    Jul 7, 2015 at 12:01
  • This answer is right, just tested it. Oct 14, 2015 at 8:49

If you know where the code is that defines the form you want to alter, look there. The form id is just the name of the function defining that form.

For example, Views defines the function views_ui_edit_view_form on line 875 of views/includes/admin.inc. The Hierarchical Select views filter looks for that form id on line 50 of hierarchical_select/modules/hs_taxonomy_views.module in a hood_form_alter function.


Use drupal_set_message() function to display the form id.

function hook_form_alter(&$form, $form_state, $form_id) {
// other codes
  • drupal_set_message($form_id); is deprecated in Drupal 10. You can use \Drupal::messenger()->addMessage($form_id); instead. Feb 9 at 8:37

Use the Get Form ID module to easily find out form id of any form in Drupal.

Here is a quote about it from its project page:

By hovering over any form you will see a contextual link available. By expanding the link you will see the form ID. Click on the link and you will see a modal window with copyable form id and hook_form_FORM_ID_alter() hook suggestion.

This module is deprecated. It was improved, extended and rebranded as Devel form debug; use it instead.

Disclosure: I'm maintainer of these modules.


As the code you reported is looking for the form ID of the node form, there are two cases.

Drupal 6

If the code is trying to alter the form used to set the settings for a content type, then it should use the following IF-statement.

if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { /* … */ }

If the code is trying to alter the node edit form, then the code should use the following IF-statement.

if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) { /* … */ }

Drupal 7 and higher

In the first case, the IF-statement should be the following one:

if ($form_id == 'node_type_form') { /* … */ }

Using a different approach, supposing that mymodule is the short name of your module, you could use mymodule_form_node_type_form_alter(&$form, &$form_state, $form_id). Since Drupal 7, all the hooks used to alter the form implemented by another module gets $form_id as last parameter. See hook_form_alter(), hook_form_FORM_ID_alter(), hook_form_BASE_FORM_ID_alter().

In the second case, the IF-statement is the same used for Drupal 6.

if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) { /* … */ }

First install Devel module.Then create a module like the code below

function hook_form_alter(&$form, &$form_state, $form_id) {

Then you can see ALL details of a form, like form ID, field names of a form etc.


In Drupal 8 you can get a form id like this. You have to write this code in your .module file

function modulename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  echo "my form id is : ".$form_id;

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.