3 namespace Drupal\{{ machine_name }}\Form;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Provides a {{ name }} form.
14 class {{ class }} extends FormBase {
19 * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
24 * Constructs a {{ class }} object.
26 * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
29 public function __construct(LoggerChannelFactoryInterface $logger_factory) {
30 $this->loggerFactory = $logger_factory;
36 public static function create(ContainerInterface $container) {
38 $container->get('logger.factory')
45 public function getFormId() {
46 return '{{ form_id }}';
52 public function buildForm(array $form, FormStateInterface $form_state) {
55 '#type' => 'textarea',
56 '#title' => $this->t('Message'),
60 $form['actions'] = ['#type' => 'actions'];
61 $form['actions']['submit'] = [
63 '#value' => $this->t('Save'),
72 public function validateForm(array &$form, FormStateInterface $form_state) {
73 if (Unicode::strlen($form_state->getValue('message')) < 10) {
74 $form_state->setErrorByName('name', $this->t('Message should be at least 10 characters.'));
81 public function submitForm(array &$form, FormStateInterface $form_state) {
82 $this->loggerFactory->get('{{ machine_name }}')->debug(
83 $form_state->getValue('message')
85 drupal_set_message($this->t('The message has been sent.'));
86 $form_state->setRedirect('system.admin');