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.
- Render API Overview
- Understand at a high level what the Render API is, when you'll encounter it, and the main components of the system. 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.
- What Are Render Arrays?
- When you use the Form API to build a form you'll construct a render array. Learn about render arrays in this tutorial.
- What Are Render Elements?
- As you build a form with the Form API, you will make use of render elements. Learn about render elements in this tutorial.
- Use #prefix and #suffix Properties to Wrap an Element
- Use Render Element Types in a Render Array
- There are a bunch of existing render elements, most commonly Form API elements. In this tutorial, learn how to locate a list of existing render element types and use any of them when defining content with a render array.
- Use #access to Show/Hide Elements in a Render Array
#accessproperty can be used an any element in a render array, including form elements, to control the visibility of that element and its children. Learn about how to make use of the #access property in this tutorial.
- Create a Settings Form in a Module
- Create a module containing default configuration, a settings form, and a menu link to the form.