Drupal's Form API is a set of interfaces, utility classes, and conventions that when combined together allow module developers to create forms that collect, validate, and process user-submitted data. The Form API is closely related to the Render API.
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.
Example tasks
- Create custom forms in Drupal
- Alter existing forms
- Configure Drupal's core Contact module-provided forms (see the External Resources section)
Confidence
While there are many similarities to the Drupal 7 Form API, Drupal's current 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 form today. It's best to use resources written for Drupal 8 and above.
Drupalize.Me resources
The Form API uses the Render API, so it's a good idea to familiarize yourself with the Render API. It may help you better understand Form API.
Drupal 7
External resources
- Introduction to the Form API (Drupal.org)
- Learn about features of the Form API in Drupal; get an overview of the process of form building; learn about the classes and interfaces for use with forms; learn how to identify your form, build, validate, and submit it; and learn to alter an existing form with a hook.
- Form generation (api.drupal.org)
- The Drupal API page describes how to generate and manipulate forms and process form submissions.
- Form and render elements (api.drupal.org)
- This list contains all of the
FormElement
render elements that you can use in a form.
- This list contains all of the
- Change record: HTML5 support for form elements (Drupal.org)
- This is a list of HTML5 form elements that were introduced in Drupal, along with code usage examples.
- Examples for Developers: Form API Example (git.drupalcode.org)
- The Examples for Developers project provides code examples of various APIs in Drupal. You can download the project and browse the examples using a code editor.
- Implementing #autocomplete in Drupal 8 with Custom Callbacks (qed42.com)
- How to implement autocomplete functionality in a form element, including utilizing a custom route and controller.
- Theming form elements in Drupal 8 (thirdandgrove.com)
- Learn about form-specific template files, preprocess functions, and how to customize the rendering of form elements.
Contact module
- Contact module overview (Drupal.org)
- Learn how to configure Drupal's built-in personal and site-wide contact forms which are provided by the core Contact module.
- Drupal Contact Form & using SMTP to Send Email (designkojo.com)
- Learn how to configure Drupal's built-in contact form and your server's SMTP settings in order to send email to your site's users via the contact form.