Introduced way back in Drupal 4.7 and evolving ever since, the Form API is essential for any module developer to master. Forms are one of the primary means through which users interact with a web application. And the Form API is how Drupal module developers create, update, and modify forms.
For many developers implementations of hook_form_api()
are the first foray into writing custom modules. No matter where you are on your journey to mastering Drupal module development you'll benefit from understanding the ins-and-outs of the Form API.
In this first set of tutorials on the Form API, we'll talk about what exactly the Form API is and the benefits it has over hard-coded HTML forms, including a detailed look at the life cycle of a form and the various points at which developers can jump in and customize things. We'll discuss the overlap between the Form API and the Render API, look at how to map a route to a form controller, and how form controllers can build, validate, and process submitted data for, forms.
We'll also look at the venerable hook_form_api()
, how to implement it, and some examples of not changing how forms look but also how they are validated and processed.
Here's the full list of tutorials we're releasing today:
- Form API Overview
- Form API Life Cycle
- Define a New Form Controller and Route
- Add Input Elements to a Form
- Form Element Reference
- Validate Form Input
- Validate a Form via the Form Controller
- Add a Validation Callback to an Existing Form
- Validate a Single Form Element
- Handle Submitted Form Data
- Process Submitted Form Data with a Callback
- Process Submitted Form Data via the Form Controller
- Inject Services into a Form Controller
- Provide Default Values for Form Elements
- Alter an Existing Form with hook_form_alter()
In the future, we'll be adding additional tutorials on:
- Submitting forms via AJAX
- Handling file uploads
- and, creating multi-step wizards with the Form API
Let us know if you've got any burning questions about Drupal's Form API.
Comments
When a user submits a form the request is made to the same URL that the form was displayed on, Drupal notices the incoming HTTP POST data in the request and this time instead of building the form and displaying it as HTML, it builds the form and then proceeds to call the applicable validation and submission handlers.
Add new comment