{% extends "base/class.php.twig" %}
{% block file_path %}
\Drupal\{{module_name}}\Form\{{ class_name }}.
{% endblock %}
{% block namespace_class %}
namespace Drupal\{{module_name}}\Form;
{% endblock %}
{% block use_class %}
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
{% if services is not empty %}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endblock %}
{% block use_class_services %}
{% endblock %}
{% block class_declaration %}
/**
* Class {{ class_name }}.
*/
class {{ class_name }} extends FormBase {% endblock %}
{% block class_create %}
{% if services is not empty %}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
{{ serviceClassInjectionNoOverride(services) }}
return $instance;
}
{% endif %}
{% endblock %}
{% block class_methods %}
/**
* {@inheritdoc}
*/
public function getFormId() {
return '{{form_id}}';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
{% for input in inputs %}
{% if input.fieldset is defined and input.fieldset|length %}
$form['{{ input.fieldset }}']['{{ input.name }}'] = [
{% else %}
$form['{{ input.name }}'] = [
{% endif %}
'#type' => '{{ input.type }}',
'#title' => $this->t('{{ input.label|e }}'),
{% if input.description is defined and input.description|length %}
'#description' => $this->t('{{ input.description|e }}'),
{% endif %}
{% if input.options is defined and input.options|length %}
'#options' => {{ input.options }},
{% endif %}
{% if input.maxlength is defined and input.maxlength|length %}
'#maxlength' => {{ input.maxlength }},
{% endif %}
{% if input.size is defined and input.size|length %}
'#size' => {{ input.size }},
{% endif %}
{% if input.default_value is defined and input.default_value|length %}
'#default_value' => '{{ input.default_value }}',
{% endif %}
{% if input.weight is defined and input.weight|length %}
'#weight' => '{{ input.weight }}',
{% endif %}
];
{% endfor %}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValues() as $key => $value) {
// @TODO: Validate fields.
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
foreach ($form_state->getValues() as $key => $value) {
{% if 'messenger' in services|keys %}
$this->messenger->addMessage($key . ': ' . ($key === 'text_format'?$value['value']:$value));
{% else %}
\Drupal::messenger()->addMessage($key . ': ' . ($key === 'text_format'?$value['value']:$value));
{% endif %}
}
}
{% endblock %}