Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / module / content-entity / src / Form / ExampleTypeForm.php.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/module/content-entity/src/Form/ExampleTypeForm.php.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/module/content-entity/src/Form/ExampleTypeForm.php.twig
new file mode 100644 (file)
index 0000000..ae3a942
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\Form;
+
+use Drupal\Core\Entity\BundleEntityFormBase;
+use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityTypeInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Form handler for {{ entity_type_label|lower }} type forms.
+ */
+class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
+
+  /**
+   * The entity manager.
+   *
+   * @var \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected $entityManager;
+
+  /**
+   * Constructs the {{ class_prefix }}TypeForm object.
+   *
+   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
+   *   The entity manager.
+   */
+  public function __construct(EntityManagerInterface $entity_manager) {
+    $this->entityManager = $entity_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('entity.manager')
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function form(array $form, FormStateInterface $form_state) {
+    $form = parent::form($form, $form_state);
+
+    $entity_type = $this->entity;
+    if ($this->operation == 'add') {
+      $form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
+    }
+    else {
+      $form['#title'] = $this->t(
+        'Edit %label {{ entity_type_label|lower }} type',
+        ['%label' => $entity_type->label()]
+      );
+    }
+
+    $form['label'] = [
+      '#title' => $this->t('Label'),
+      '#type' => 'textfield',
+      '#default_value' => $entity_type->label(),
+      '#description' => $this->t('The human-readable name of this {{ entity_type_label|lower }} type.'),
+      '#required' => TRUE,
+      '#size' => 30,
+    ];
+
+    $form['id'] = [
+      '#type' => 'machine_name',
+      '#default_value' => $entity_type->id(),
+      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
+      '#machine_name' => [
+        'exists' => ['Drupal\{{ machine_name }}\Entity\{{ class_prefix }}Type', 'load'],
+        'source' => ['label'],
+      ],
+      '#description' => $this->t('A unique machine-readable name for this {{ entity_type_label|lower }} type. It must only contain lowercase letters, numbers, and underscores.'),
+    ];
+
+    return $this->protectBundleIdElement($form);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function actions(array $form, FormStateInterface $form_state) {
+    $actions = parent::actions($form, $form_state);
+    $actions['submit']['#value'] = $this->t('Save {{ entity_type_label|lower }} type');
+    $actions['delete']['#value'] = $this->t('Delete {{ entity_type_label|lower }} type');
+    return $actions;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function save(array $form, FormStateInterface $form_state) {
+    $entity_type = $this->entity;
+
+    $entity_type->set('id', trim($entity_type->id()));
+    $entity_type->set('label', trim($entity_type->label()));
+
+    $status = $entity_type->save();
+
+    $t_args = ['%name' => $entity_type->label()];
+
+    if ($status == SAVED_UPDATED) {
+      $message = 'The {{ entity_type_label|lower }} type %name has been updated.';
+    }
+    elseif ($status == SAVED_NEW) {
+      $message = 'The {{ entity_type_label|lower }} type %name has been added.';
+    }
+    drupal_set_message($this->t($message, $t_args));
+
+    $this->entityManager->clearCachedFieldDefinitions();
+    $form_state->setRedirectUrl($entity_type->urlInfo('collection'));
+  }
+
+}