mcpField
D
drupal-form-api

drupal-form-api

github.com/edutrulDevelopment7349 installsUpdated 21w ago
Repository

About drupal-form-api

A Form API reference for Drupal developers, with examples of standard and configuration form structures. It also lists common form elements and their purposes.

Key features

  1. 01Build FormBase forms
  2. 02Build ConfigFormBase forms
  3. 03Define config names and form IDs
  4. 04Reference common form elements

Use cases

  1. 01Create interactive module forms
  2. 02Build module settings pages
  3. 03Choose suitable input elements

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-form-api

After installing, type/drupal-form-apiin Claude Code to confirm it loaded.

Bundled filesREADME.md

Details

From the project README

Drupal Form API

Simple Form (FormBase)

// src/Form/MyForm.php
namespace Drupal\my_module\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class MyForm extends FormBase {

  public function __construct(
    private readonly EntityTypeManagerInterface $entityTypeManager,
  ) {}

  public static function create(ContainerInterface $container): static {
    return new static(
      $container->get('entity_type.manager'),
    );
  }

  public function getFormId(): string {
    return 'my_module_my_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#required' => TRUE,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];
    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $name = $form_state->getValue('name');
    $this->messenger()->addStatus($this->t('Hello @name!', ['@name' => $name]));
  }

}

Config Form (ConfigFormBase)

final class MySettingsForm extends ConfigFormBase {

  protected function getEditableConfigNames(): array {
    return ['my_module.settings'];
  }

  public function getFormId(): string {
    return 'my_module_settings';
  }

  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config('my_module.settings');
    $form['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable feature'),
      '#default_value' => $config->get('enabled'),
    ];
    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->config('my_module.settings')
      ->set('enabled', $form_state->getValue('enabled'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Common Form Elements

TypeDescription
textfieldSingle line text
textareaMulti-line text
selectDropdown
checkboxesMultiple checkboxes
radiosRadio buttons
checkboxSingle checkbox
numberNumeric input
emailEmail input
managed_fileManaged file upload
entity_autocompleteEntity reference autocomplete
dateDate picker
detailsCollapsible group
containerNon-visual wrapper
hiddenHidden field
submitSubmit button

Comments

Markdown supported