22

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';
  }
}
  • 2
    Possible Duplicate: drupal.stackexchange.com/questions/5605/… – Laxman13 Jun 24 '11 at 17:57
  • please see the answers to the question above, it should also answer your question – Laxman13 Jun 24 '11 at 17:58
  • 1
    The question here is quite different; it's not asking how to identify a form element, but which is the form ID that should be checked in the implementation of hook_form_alter(). Given also the reported code, I would say this question is different. – kiamlaluno Jun 26 '11 at 13:37
16

Try printing the form variables with PHP function print_r.

function hook_form_alter(&$form, &$form_state, $form_id) {
  print_r($form);
}

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. – kiamlaluno Jun 26 '11 at 13:52
  • 9
    Also $form_id is a param you can directly print it !! – niksmac Apr 16 '12 at 11:19
  • 1
    If you have Devel enabled, just dpm($form_id); – No Sssweat Jun 7 '17 at 7:43
34

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 :)

  • 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 '18 at 20:58
10

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. – kiamlaluno Sep 3 '12 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 '15 at 12:01
  • This answer is right, just tested it. – vicenteherrera Oct 14 '15 at 8:49
6

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.

4

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.

3

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) { /* … */ }
3

Use drupal_set_message() function to display the form id.

function hook_form_alter(&$form, $form_state, $form_id) {
drupal_set_message($form_id);
// other codes
}
1

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

function hook_form_alter(&$form, &$form_state, $form_id) {
  dpm($form);
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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