Creating a form

In this recipe, we will create a form, which will be accessible from a menu path. This will involve creating a route that tells Drupal to invoke our form and display it to the end user.

Forms are defined as classes, which implement \Drupal\Core\Form\FormInterface. The \Drupal\Core\Form\FormBase serves as a utility class that is intended to be extended. We will extend this class to create a new form.

Getting ready

Since we will be writing the code, you will want to have a custom module. Creating a custom module in Drupal is simply creating a folder and an info.yml file. For this recipe, we will create a folder under /modules in your Drupal folder called drupalform.

In the drupalform folder, create drupalform.info.yml. The info.yml

Get Drupal 8: Enterprise Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.