How can I find the form ID of a form?

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';

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.

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

  • If your webform is located at "" then your form ID will be "webform_client_form_351". So, whatever your nid is - that's your form ID.

  • 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/ 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

  • 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;

Related Questions