Problem/Motivation

Updating this module to fully prepare it for Drupal Core merge consideration.

Proposed resolution

Form Factory Kits or similar object-oriented form functionality provided by Drupal Core.

Remaining tasks

  • 90%+ Test Coverage
  • Alpha release

API changes

  • Addition of `FormObjectBase` class as shown here.

Comments

yesnoio created an issue. See original summary.

yesnoio’s picture

Several examples, like the one below, are documented here.


namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\formfactory\Services\FormFactoryInterface;
use Drupal\formfactorykits\Services\FormFactoryKitsInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ExampleForm extends FormBase {

  private FormFactoryInterface $formFactoryService;

  private FormFactoryKitsInterface $formFactoryKitsService;

  public function __construct(FormFactoryInterface $formFactoryService,
                              FormFactoryKitsInterface $formFactoryKits)
  {
    $this->formFactoryService = $formFactoryService;
    $this->formFactoryKitsService = $formFactoryKits;
  }

  public static function create(ContainerInterface $container)
  {
    return new static(
      $container->get('formfactory'),
      $container->get('formfactorykits')
    );
  }

  public function getFormId() {
    return 'example_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $factory = $this->formFactoryService->load($form);
    $k = $this->formFactoryKitsService;

    // Create VerticalTabsKit & add it to the FormFactory instance.
    $tabs = $k->verticalTabs();
    $factory->append($tabs);
    
    // Create a "dogs" TabKit & add it to the VerticalTabsKit instance.
    $dogsTab = $tabs->createTab('dogs')
      ->setTitle($this->t('Dogs'));
    $dogsTab->append(
      $k->image('dogs_image')
        ->setTitle($this->t('Image'))
    );
    $dogsTab->append(
      $k->textarea('dogs_description')
        ->setTitle($this->t('Description'))
    );
    $dogsTab->append(
      $k->checkboxes('dogs_attributes')
        ->setTitle($this->t('Attributes'))
        ->appendOption(['a' => $this->t('A')])
        ->appendOption(['b' => $this->t('B')])
        ->appendOption(['c' => $this->t('C')])
        ->setDefaultValue(['b'])
    );

    // Create a "cats" TabKit & add it to the VerticalTabsKit instance.
    $tabs->createTab('cats')
      ->setTitle($this->t('Cats'))
      ->append($k->image('cats_image')->setTitle($this->t('Image')))
      ->append($k->textarea('cats_description')->setTitle($this->t('Description')));

    // Create a SubmitKit & add it to the FormFactory instance.
    $factory->append($k->submit());

    // Return the modified $form render array.    
    return $factory->getForm();
  }
  
  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    // TODO: implement method
    throw new \BadMethodCallException();
  }
}

The newer (optional) `FormObjectBase` helps make forms more succinct.


namespace Drupal\example\Form;

use Drupal\formfactorykits\Form\FormObjectBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\formfactory\Services\FormFactoryInterface;
use Drupal\formfactorykits\Services\FormFactoryKitsInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ExampleForm.
 *
 * @package Drupal\example\Form
 */
class ExampleForm extends FormObjectBase {

  public function getFormId()
  {
    return 'example_form';
  }

  public function buildFormObject(FormFactoryInterface $form_factory, FormFactoryKitsInterface $kits): void
  {
    // Optional: the Form State object can be accessed via the Form Factory.
    $form_state = $form_factory->getFormState();

    // Create VerticalTabsKit & add it to the FormFactory instance.
    $tabs = $kits->verticalTabs();
    $form_factory->append($tabs);

    // Create a "dogs" TabKit & add it to the VerticalTabsKit instance.
    $dogsTab = $tabs->createTab('dogs')
      ->setTitle($this->t('Dogs'));
    $dogsTab->append(
      $kits->image('dogs_image')
        ->setTitle($this->t('Image'))
    );
    $dogsTab->append(
      $kits->textarea('dogs_description')
        ->setTitle($this->t('Description'))
    );
    $dogsTab->append(
      $kits->checkboxes('dogs_attributes')
        ->setTitle($this->t('Attributes'))
        ->appendOption(['a' => $this->t('A')])
        ->appendOption(['b' => $this->t('B')])
        ->appendOption(['c' => $this->t('C')])
        ->setDefaultValue(['b'])
    );

    // Create a "cats" TabKit & add it to the VerticalTabsKit instance.
    $tabs->createTab('cats')
      ->setTitle($this->t('Cats'))
      ->append($kits->image('cats_image')->setTitle($this->t('Image')))
      ->append($kits->textarea('cats_description')->setTitle($this->t('Description')));

    // Create a SubmitKit & add it to the FormFactory instance.
    $form_factory->append($kits->submit());
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    // TODO: implement method.
    throw new \BadMethodCallException();
  }

}