Drupal 8 has a powerful Form API that makes the form building process relatively straightforward. Web designers can use forms for many purposes on the website, such as adding a person to a newsletter or receiving messages from visitors.
The designers can set up these forms as a structured array using Drupal 8. This method makes the submitted information consistent between all of the forms. They also have the capability to share forms in multiple modules without a complicated process.
Three Types of Forms
Drupal 8 supports three types of forms. A generic form is versatile and applies to many common use cases that a developer needs for a website. This approach uses FormBase.
If the designer wants to set up a form for administrators to configure modules, the ConfigFormBase enables this.
The third form is for form submissions that require a confirmation before changing a setting or removing content. Designers use ConfirmFormBase for this.
When using any of these bases, Drupal requires three methods:
- getFormId(): This method returns the unique identifier of the form. Use a descriptive name so it’s easy to see which form it is.
- buildForm(): This method gives the designer the array with all of the form elements. For example, the name, phone number, and comments field.
- submitForm(): This method controls what happens when the form goes through the submission process.
Setting Up a Drupal 8 Form
The first thing that a Drupal 8 form needs is a unique identifier. This is the string that gets returned as part of this function. The designer also needs to know what elements the form needs so they can include them in the buildForm() method. Once the form is built, the designer can configure it in many different ways to expand the functionality of it.
Data validation ensures that no errors are present when the form gets submitted by the end user. This method is also useful for preventing certain types of attacks, such as hackers that attempt to send database queries through the form fields. Another advantage to form validation is ensuring that end users are putting the right type of information in each field. For example, one can limit the phone number field to numbers-only.
If the validation method finds errors, then the form appears with the invalid fields marked in reds. The end user can change their submission so that it meets the validation rules.
The validateForm() method is used for this process.
Submitting a Drupal 8 Form
The submitForm() method handles the final step of the Drupal 8 form building process. At this point, the end user has entered all of the information and it’s gone through validation. Now the form needs to know where to send the submitted information and any other steps that come after that.
Designers have other ways that they can manipulate forms and publish them on Drupal modules. Some add-ons are also available to streamline the form building method. The number of uses that forms have are almost endless, whether the Drupal site is used for an employee intranet or as a public facing website. Lead generation forms, contact forms, information request forms, appointment booking and more all cover ways that designers can use this powerful built-in Drupal functionality.