Car Assembly Line
Began
Dependencies

Make custom Drupal forms... faster.

This module provides a service capable of creating FormFactoryKit objects.

Each Kit is able to generate an array compatible with Drupal's Render API.

When appended to a FormFactory instance (provided by formfactory module), the Kit is used to help create a complete form.

The kits provided by this module are merely covering Form API basics. Collections of Kit objects can be provided by any module.

The Basics

$factory = $this->formFactoryService->load($form);
$k = $this->formFactoryKitsService;

// Create a `textfield` field.
$factory->append($k->text());

// Create a `managed_file` field.
$factory->append($k->file());

// Create a `submit` button.
$factory->append($k->submit());

// Build the render array.
$form = $factory->getForm();

 

In A Form

<?php

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();
  }
}

 

$button = $k->button('foo')
  ->setValue($this->t('Foo'));

 

$checkboxes = $k->checkboxes('foo')
  ->setTitle($this->t('Foo'))
  ->setDescription($this->t('Foo description.'))
  ->appendOption('bar', $this->t('Bar')),
  ->appendOption('baz', $this->t('Baz')),
  ->appendOption('qux', $this->t('Qux')),
  ->setDefaultValue(['baz']);

 

$checkbox = $k->checkbox('foo')
  ->setTitle($this->t('Foo'));

 

$color = $k->color('color_background')
  ->setTitle($this->t('Background Color Picker'));

 

$container = $k->container('buttons')
  ->append(
    $k->button('cancel')
      ->setValue($this->t('Cancel'))
  )
  ->append(
    $k->submit()
  );

 

$date = $k->date('foo')
  ->setTitle($this->t('Foo'));

 

$dateList = $k->dateList('foo')
  ->setTitle($this->t('Foo'));

 

$dateTime = $k->dateTime('foo')
  ->setTitle($this->t('Foo'));

 

$email = $k->email('foo_email')
  ->setTitle($this->t('Email'));

 

$file = $k->file('foo_file')
  ->setTitle($this->t('File'))
  ->setMultiple();

 

$fileUnmanaged = $k->fileUnmanaged('foo_file')
  ->setTitle($this->t('File'))
  ->setMultiple();

 

$hidden = $k->hidden('foo')
  ->setValue('bar');

 

$button = $k->imageButton('foo_button')
  ->setAlternativeText($this->t('Foo'))
  ->setSource('core/misc/icons/787878/cog.svg');

 

$image = $k->image()
  ->setTitle($this->t('Image');

 

$item = $k->item('foo_item')
  ->setTitle($this->t('Foo'))
  ->setMarkup('<b>bar</b>');

 

$nodeAutoComplete = $k->nodeAutoComplete('foo_node')
  ->setTitle($this->t('Foo Node'))
  ->setTargetBundle('foo')
  ->setCondition(
    $q->condition('status')->is()
  )
  ->setCondition(
    $q->condition('field_bar')->isIn(['x', 'y', 'z'])
  );

 

$number = $k->number('foo_number')
  ->setTitle($this->t('Foo Number'));

 

$passwordConfirm = $k->passwordConfirm();

 

$password = $k->password();

 

$path = $k->path('foo_path')
  ->setTitle($this->t('Path'))
  ->setConversion(PathElement::CONVERT_URL);

 

$radios = $k->radios('name')
  ->setTitle($this->t('Pick a Name'))
  ->setOptions([
    'henry' => 'Henry Jones',
    'anna' => 'Anna Jones',
    'jr' => 'Henry Jones Jr.',
  ]);

 

$range = $k->range('foo_range')
  ->setTitle($this->t('Range'))
  ->setMinimum(0)
  ->setMaximum(10)
  ->setStep(2);

 

$search = $k->search('foo')
  ->setTitle($this->t('Search'));

 

$select = $k->select('name')
  ->setTitle($this->t('Pick a Name'))
  ->setOptions([
    'henry' => 'Henry Jones',
    'anna' => 'Anna Jones',
    'jr' => 'Henry Jones Jr.',
  ]);

 

$submit = $k->submit()
  ->setValue($this->t('Go'));

 

$table = $k->table('foo_table')
  ->setTitle($this->t('Foo Table'))
  ->appendHeaderColumn($this->t('Bar'))
  ->appendHeaderColumn($this->t('Baz'))
  ->appendHeaderColumn($this->t('Qux'))
  ->appendRow(['a', 'b', 'c']);

 

$tableSelect = $k->tableSelect('names')
  ->setTitle($this->t('Names'))
  ->appendHeaderColumn('first', $this->t('First Name'))
  ->appendHeaderColumn('last', $this->t('Last Name'))
  ->appendHeaderColumn('suffix', $this->t('Suffix'))
  ->appendHeaderColumn('nickname', $this->t('Nickname'))
  ->setOptions([
    'henry' => [
      'first' => 'Henry',
      'last' => 'Jones',
    ],
    'anna' => [
      'first' => 'Anna',
      'last' => 'Jones',
    ],
  ])
  ->appendOption('jr', [
    'first' => 'Henry',
    'last' => 'Jones',
    'suffix' => 'Jr.',
    'nickname' => 'Indiana',
  ]);

 

$select = $k->taxonomyTermSelect('foo_tag')
  ->loadTaxonomyVocabulary('tags');

$selectMultiple = $k->taxonomyTermSelect('bar_tags')
  ->setMultiple()
  ->loadTaxonomyVocabulary('tags');

 

$telephone = $k->telephone('foo')
  ->setTitle($this->t('Foo Telephone'));

 

$textarea = $k->textarea('foo')
  ->setTitle($this->t('Foo'))
  ->setDescription($this->t('Foo description.'))
  ->setDefaultValue('Foo value.')

 

$text = $k->text('foo')
  ->setTitle($this->t('Foo'));

 

$url = $k->url('foo')
  ->setTitle($this->t('Foo URL'));

 

$userAutoComplete = $k->userAutoComplete('foo_user')
  ->setTitle($this->t('Foo User'))
  ->setCondition(
    $q->condition('uid')->isNotEqualTo(1)
  );

 

$value = $k->value('foo')
  ->setValue('bar');

 

// Create VerticalTabsKit.
$tabs = $k->verticalTabs();

// 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')));