Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / form / simple.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/form/simple.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/form/simple.twig
new file mode 100644 (file)
index 0000000..63475c8
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\Form;
+
+use Drupal\Component\Utility\Unicode;
+use Drupal\Core\Form\FormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Provides a {{ name }} form.
+ */
+class {{ class }} extends FormBase {
+
+  /**
+   * The logger factory.
+   *
+   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
+   */
+  protected $logger;
+
+  /**
+   * Constructs a {{ class }} object.
+   *
+   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
+   *   A logger instance.
+   */
+  public function __construct(LoggerChannelFactoryInterface $logger_factory) {
+    $this->loggerFactory = $logger_factory;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('logger.factory')
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getFormId() {
+    return '{{ form_id }}';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildForm(array $form, FormStateInterface $form_state) {
+
+    $form['message'] = [
+      '#type' => 'textarea',
+      '#title' => $this->t('Message'),
+      '#required' => TRUE,
+    ];
+
+    $form['actions'] = ['#type' => 'actions'];
+    $form['actions']['submit'] = [
+      '#type' => 'submit',
+      '#value' => $this->t('Save'),
+    ];
+
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateForm(array &$form, FormStateInterface $form_state) {
+    if (Unicode::strlen($form_state->getValue('message')) < 10) {
+      $form_state->setErrorByName('name', $this->t('Message should be at least 10 characters.'));
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    $this->loggerFactory->get('{{ machine_name }}')->debug(
+      $form_state->getValue('message')
+    );
+    drupal_set_message($this->t('The message has been sent.'));
+    $form_state->setRedirect('system.admin');
+  }
+
+}