$npx -y skills add edutrul/drupal-ai --skill drupal-form-apiDrupal Form API — building forms with FormBase/ConfigFormBase, form elements, validate/submit handlers, DI, and generating forms with Drush including routes.
| 1 | # Drupal Form API |
| 2 | |
| 3 | ## Simple Form (FormBase) |
| 4 | |
| 5 | ```php |
| 6 | // src/Form/MyForm.php |
| 7 | namespace Drupal\my_module\Form; |
| 8 | |
| 9 | use Drupal\Core\Form\FormBase; |
| 10 | use Drupal\Core\Form\FormStateInterface; |
| 11 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 12 | |
| 13 | final class MyForm extends FormBase { |
| 14 | |
| 15 | public function __construct( |
| 16 | private readonly EntityTypeManagerInterface $entityTypeManager, |
| 17 | ) {} |
| 18 | |
| 19 | public static function create(ContainerInterface $container): static { |
| 20 | return new static( |
| 21 | $container->get('entity_type.manager'), |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | public function getFormId(): string { |
| 26 | return 'my_module_my_form'; |
| 27 | } |
| 28 | |
| 29 | public function buildForm(array $form, FormStateInterface $form_state): array { |
| 30 | $form['name'] = [ |
| 31 | '#type' => 'textfield', |
| 32 | '#title' => $this->t('Name'), |
| 33 | '#required' => TRUE, |
| 34 | ]; |
| 35 | $form['submit'] = [ |
| 36 | '#type' => 'submit', |
| 37 | '#value' => $this->t('Submit'), |
| 38 | ]; |
| 39 | return $form; |
| 40 | } |
| 41 | |
| 42 | public function submitForm(array &$form, FormStateInterface $form_state): void { |
| 43 | $name = $form_state->getValue('name'); |
| 44 | $this->messenger()->addStatus($this->t('Hello @name!', ['@name' => $name])); |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ## Config Form (ConfigFormBase) |
| 51 | |
| 52 | ```php |
| 53 | final class MySettingsForm extends ConfigFormBase { |
| 54 | |
| 55 | protected function getEditableConfigNames(): array { |
| 56 | return ['my_module.settings']; |
| 57 | } |
| 58 | |
| 59 | public function getFormId(): string { |
| 60 | return 'my_module_settings'; |
| 61 | } |
| 62 | |
| 63 | public function buildForm(array $form, FormStateInterface $form_state): array { |
| 64 | $config = $this->config('my_module.settings'); |
| 65 | $form['enabled'] = [ |
| 66 | '#type' => 'checkbox', |
| 67 | '#title' => $this->t('Enable feature'), |
| 68 | '#default_value' => $config->get('enabled'), |
| 69 | ]; |
| 70 | return parent::buildForm($form, $form_state); |
| 71 | } |
| 72 | |
| 73 | public function submitForm(array &$form, FormStateInterface $form_state): void { |
| 74 | $this->config('my_module.settings') |
| 75 | ->set('enabled', $form_state->getValue('enabled')) |
| 76 | ->save(); |
| 77 | parent::submitForm($form, $form_state); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ## Common Form Elements |
| 84 | |
| 85 | | Type | Description | |
| 86 | |---|---| |
| 87 | | `textfield` | Single line text | |
| 88 | | `textarea` | Multi-line text | |
| 89 | | `select` | Dropdown | |
| 90 | | `checkboxes` | Multiple checkboxes | |
| 91 | | `radios` | Radio buttons | |
| 92 | | `checkbox` | Single checkbox | |
| 93 | | `number` | Numeric input | |
| 94 | | `email` | Email input | |
| 95 | | `managed_file` | Managed file upload | |
| 96 | | `entity_autocomplete` | Entity reference autocomplete | |
| 97 | | `date` | Date picker | |
| 98 | | `details` | Collapsible group | |
| 99 | | `container` | Non-visual wrapper | |
| 100 | | `hidden` | Hidden field | |
| 101 | | `submit` | Submit button | |