欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal7 d7 How can I find the form ID of a form? 如何发现 查找 form_id 有大用 有大大用

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

18

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.

42

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.  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 
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.

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.

5

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

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.

0

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


来自 https://drupal.stackexchange.com/questions/5802/how-can-i-find-the-form-id-of-a-form


普通分类: