Module Development

Provide Default Values for Form Elements for Drupal 8, 9, 10, and 11

Forms are used for both collecting new data, and editing existing data. In order to allow users to modify existing data you need to pre-populate the elements on the form with the data you previously stored.

In this tutorial we'll look at the ways in which forms can be pre-populated with existing data, including:

  • Providing default values for form elements which a user can edit with the #default_value property
  • The differences between the #value and #default_value properties

By the end of this tutorial you should know how to populate forms using existing data.