My highly-n00b question is: suppose I have a form callback which could be used as argument to drupal_get_form().

How can I specify an ID for the form so I can compare it inside hook_form_alter against the given-as-argument $form_id?

This means: I want to give a form id to the form, e.g. "my-system-form", and distinguish it to apply some alter-logic (don't want to apply that logic to the rest of the forms).

1 Answer 1


hook_form_alter() has $form_id as an argument:

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

Which is:

String representing the name of the form itself. Typically this is the name of the function that generated the form.

(emphasis mine)

To understand the "typically" in there, have a read through the docs for hook_forms() (chances are it won't apply to your situation, or if it does, you probably already know about it).

So, generally speaking, you can use:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'MYMODULE_some_form') {

Or, the more specific hook_form_FORM_ID_alter(), which will only be invoked for that specific form:

function MYMODULE_form_MYMODULE_some_form_alter(&$form, &$form_state, $form_id) {
  • My question is: when declaring the form and returning it as an array: where do I specify 'MYMODULE_some_form'? It's the name of the function creating the form specs? (i.e. the array) Apr 7, 2014 at 17:55
  • 1
    Exactly right - so if your form callback function is function MYMODULE_some_form($form, &$form_state) { ..., 'MYMODULE_some_form' is the string you'll get in $form_id. Don't forget, you're passing that string to drupal_get_form() already, it's just being re-used
    – Clive
    Apr 7, 2014 at 17:57

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.