Module Development

Alter an Existing Form with hook_form_alter() for Drupal 8, 9, and 10

You'll often need to make minor, or major, alterations to an existing form provided by another module. The Form API allows you to alter any existing form through a series of hooks without having to change the existing module's code at all. This is probably one of the most powerful features of the Drupal Form API. Knowing how to implement and leverage hook_form_alter() and its variations is an essential skill for any module developer.

In this tutorial we'll:

  • Learn how to implement hook_form_alter() and hook_form_FORM_ID_alter() in a module
  • Modify existing elements, or add new elements, to a form provided by another module
  • Understand how to add new validation and submission handlers when altering an existing form

By the end of this tutorial you should know how to alter almost everything about the way any form in Drupal works without having to hack the module that provides the form.

Drupal Module Development