How to do it...

  1. Create an src folder in your module directory. In this directory, create a Form directory, which will hold the class that defines your form.
  2. Next, create a file called ExampleForm.php in your module's src/Form directory.
Drupal utilizes PSR4 to discover and autoload classes. For brevity, this defines that there should be one class per file, with each filename matching the class name. The folder structure will also mimic the namespace expected.
  1. We will edit the ExampleForm.php file and add the proper PHP namespace, classes used, and the class itself:
namespace Drupal\drupalform\Form; 
use Drupal\Core\Form\FormBase; 
use Drupal\Core\Form\FormStateInterface; 
class ExampleForm extends FormBase { 

The namespace defines ...

Get Drupal 8 Development Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.