Module Development

Add Input Elements to a Form for Drupal 8, 9, and 10

Module developers can add new elements to a form by adding their definition to the $form array in the buildForm() method of their controller or via an implementation of hook_form_alter(). Doing so requires knowing the element #type, and details about any element-type-specific properties.

In this tutorial we'll:

  • Determine the element type to use for the HTML input element we want to use
  • Consult the documentation for the two form element types we're using
  • Add a checkbox and a select list to our form via the buildForm() method of our form controller

By the end of this tutorial you'll know how to add new elements to an existing $form array in order to collect additional data from users.

Drupal Module Development