Same name and namespace in other branches
- 10 core/lib/Drupal/Core/Form/form.api.php \hook_form_FORM_ID_alter()
- 7.x modules/system/system.api.php \hook_form_FORM_ID_alter()
- 8.9.x core/lib/Drupal/Core/Form/form.api.php \hook_form_FORM_ID_alter()
- 9 core/lib/Drupal/Core/Form/form.api.php \hook_form_FORM_ID_alter()
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.
Note that this hook fires before hook_form_alter(). Therefore all implementations of hook_form_FORM_ID_alter() will run before all implementations of hook_form_alter(), regardless of the module order.
Parameters
$form: Nested array of form elements that comprise the form. The arguments that drupal_get_form() was originally called with are available in the array $form['#parameters'].
$form_state: A keyed array containing the current state of the form.
See also
Related topics
1 function implements hook_form_FORM_ID_alter()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- book_form_node_delete_confirm_alter in modules/
book/ book.module - Form altering function for the confirm form for a single node deletion.
File
- developer/
hooks/ core.php, line 803 - These are the hooks that are invoked by the Drupal core.
Code
function hook_form_FORM_ID_alter(&$form, &$form_state) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register" 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,
);
}