63475c80838dc07c419299885d5677e5e02e99ec
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / form / simple.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Form;
4
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;
10
11 /**
12  * Provides a {{ name }} form.
13  */
14 class {{ class }} extends FormBase {
15
16   /**
17    * The logger factory.
18    *
19    * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
20    */
21   protected $logger;
22
23   /**
24    * Constructs a {{ class }} object.
25    *
26    * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
27    *   A logger instance.
28    */
29   public function __construct(LoggerChannelFactoryInterface $logger_factory) {
30     $this->loggerFactory = $logger_factory;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('logger.factory')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return '{{ form_id }}';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildForm(array $form, FormStateInterface $form_state) {
53
54     $form['message'] = [
55       '#type' => 'textarea',
56       '#title' => $this->t('Message'),
57       '#required' => TRUE,
58     ];
59
60     $form['actions'] = ['#type' => 'actions'];
61     $form['actions']['submit'] = [
62       '#type' => 'submit',
63       '#value' => $this->t('Save'),
64     ];
65
66     return $form;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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.'));
75     }
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function submitForm(array &$form, FormStateInterface $form_state) {
82     $this->loggerFactory->get('{{ machine_name }}')->debug(
83       $form_state->getValue('message')
84     );
85     drupal_set_message($this->t('The message has been sent.'));
86     $form_state->setRedirect('system.admin');
87   }
88
89 }