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.
Since we will write the code, you will want to have a custom module. Creating a custom module in Drupal is simple: create 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
. Drupal will parse the info.yml
file to discover modules. An example of a module's info.yml
file is as follows:
name: Drupal form example description: Create a basic Drupal form, accessible from a route type: module version: 1.0 core: 8.x
The name will be your module...