hook_form_FORM_ID_alter

6 core.php hook_form_FORM_ID_alter(&$form, &$form_state)
7 system.api.php hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
8 system.api.php hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)

Provide a form-specific alteration instead of the global hook_form_alter().

Modules can implement hook_form_FORM_ID_alter() to modify a specific form, rather than implementing hook_form_alter() and checking the form ID, or using long switch statements to alter multiple forms.

Form alter hooks are called in the following order: hook_form_alter(), hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter(). See hook_form_alter() for more details.

Parameters

$form: Nested array of form elements that comprise the form.

$form_state: A keyed array containing the current state of the form. The arguments that drupal_get_form() was originally called with are available in the array $form_state['build_info']['args'].

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

See also

hook_form_alter()

hook_form_BASE_FORM_ID_alter()

drupal_prepare_form()

Related topics

File

modules/system/system.api.php, line 1652
Hooks provided by Drupal core and the System module.

Code

<?php
function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  // Modification for the form with the given form ID goes here. For example, if
  // FORM_ID is "user_register_form" this code would run only on the user
  // registration form.

  // Add a checkbox to registration form about agreeing to terms of use.
  $form['terms_of_use'] = array(
    '#type' => 'checkbox', 
    '#title' => t("I agree with the website's terms and conditions."), 
    '#required' => TRUE,
  );
}
?>

Comments

Add hook_form_FORM_ID_alter() implementations to a .inc file ...

Here's an easy way to get ALL hook_form_FORM_ID_alter() implementations loaded from a .inc file, WITHOUT declaring them each separately in hook_hook_info() ...

In the file MY_MODULE.module add:

<?php
/**
* implimentation of hook_hook_info_alter()
*/
function MY_MODULE_hook_info_alter(&$hooks) {
 
$hooks['form_alter']['group'] = 'form';
}
?>

Then in the file MY_MODULE.form.inc make sure to add:

<?php
/**
* implimentation of hook_form_alter()
*/
function MY_MODULE_form_alter(&$form, &$form_state, $form_id){
}
// add all MY_MODULE_form_FORM_ID_alter() functions bellow ...
?>

With the above function MY_MODULE_form_alter() declared in this .inc file (even if it's empty), ALL MY_MODULE_form_FORM_ID_alter() implementations declared in the same file will get loaded properly. WITHOUT this function the file will not be loaded at all!

This works because, in drupal_prepare_form() calls hook_form_alter() (and loads this .inc file) before it calls hook_form_FORM_ID_alter() for each form build.

brilliant.

brilliant.

Clear cache to see changes.

To see the results of your form alteration go to configuration and clear cache.

Form specific functions always run first.

hook_form_FORM_ID_alter() functions always run before hook_form_alter() regardless of module weight etc. Hope that saves someone else the time I wasted :)

You have it backwards - the

You have it backwards - the general hook_form_alter fires BEFORE the specific hook_form_FORM_ID_alter() other wise there would be no point in have the specific form alter hook.

What's the point of the form_id parameter?

Anyone know why form_id was added as a parameter in Drupal 7? Seems unnecessary given that we're already specifying the form_id to alter.

Trying to use

Trying to use hook_form_FORM_ID_alter() with node forms is a pain because the form ID is always "[node-type]_node_form" - that is, it's different for every node type. The Drupal 6 workaround is to use hook_form_alter() and check the form to see if it's a node form (doing something like isset($form['#node'])), but in D7, there's a better way; you can use hook_form_BASE_FORM_ID_alter(). With nodes, the base form ID is "node_form," so you can just create an implementation of hook_form_node_form_alter(). Ah, much better.

Login or register to post comments