8.2.x form.api.php hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
8.0.x form.api.php hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
8.1.x form.api.php hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
8.3.x form.api.php hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
6.x core.php hook_form_FORM_ID_alter(&$form, &$form_state)
7.x 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().

Implementations are responsible for adding cache contexts/tags/max-age as needed. See https://www.drupal.org/developing/api/8/cache.

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.


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

$form_state: The current state of the form. The arguments that \Drupal::formBuilder()->getForm() was originally called with are available in the array $form_state->getBuildInfo()['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




Related topics

56 functions implement 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.

automated_cron_form_system_cron_settings_alter in core/modules/automated_cron/automated_cron.module
Implements hook_form_FORM_ID_alter() for the system_cron_settings() form.
block_form_form_test_alter_form_alter in core/modules/system/tests/modules/form_test/form_test.module
Implements hook_form_FORM_ID_alter() on behalf of block.module.
book_form_node_confirm_form_alter in core/modules/book/book.module
Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\Form\NodeDeleteForm.
book_form_node_form_alter in core/modules/book/book.module
Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
color_form_system_theme_settings_alter in core/modules/color/color.module
Implements hook_form_FORM_ID_alter().

... See full list


core/lib/Drupal/Core/Form/form.api.php, line 245
Callbacks and hooks related to form system.


function hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $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,


atul4drupal’s picture

What is the best way to print_r $form in hook_alter function.

On my Drupal 8 setup I tried it as per drupal 7, but every time my page become unresponsive.
In my hook_form_FORM_ID_alter(), I tried

print_r($form); die;

But on loading the form the page become unresponsive.

Thanks in advance for any help.

AndyD328’s picture

Install devel and the kint submodule - https://www.drupal.org/project/devel
then use kint($form) in your code in the place of print_r and you will get a navigable data dump.

amutylo’s picture

Any possibilities for do not use hook_form_id_alter
for any form alteration in drupal 8.2?