Drupal provides a standard, secured method for module developers to add forms to a Drupal website. This method involves building render arrays in PHP and using predefined keys to specify various form element properties. The Form API describes properties for use in form arrays as well as base classes to extend, interfaces to implement, and methods for identifying, building, validating, and submitting the form.
The Configuration API also includes a special class for handling configuration settings forms, the ConfigFormBase class. See the Configuration API for Developers topic for more details about implementing configuration forms in Drupal 8.
- Create custom forms in Drupal 8
- Alter existing forms
While there are many similarities to the Drupal 7 Form API, the Drupal 8 API is distinct, mostly in its implementation of object-oriented PHP. When browsing resources on the Form API, tutorials based on Drupal 7's Form API will likely need at least some minor adjustments in order to work in a Drupal 8 form.
The Form API uses the Render API, so it's a good idea to familiarize yourself with the Render API before diving into building forms programmatically in Drupal.