ae3a94231edc0a69e8bbe6e83ff91e498fe96862
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / module / content-entity / src / Form / ExampleTypeForm.php.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Form;
4
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Form handler for {{ entity_type_label|lower }} type forms.
13  */
14 class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
15
16   /**
17    * The entity manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * Constructs the {{ class_prefix }}TypeForm object.
25    *
26    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
27    *   The entity manager.
28    */
29   public function __construct(EntityManagerInterface $entity_manager) {
30     $this->entityManager = $entity_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('entity.manager')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function form(array $form, FormStateInterface $form_state) {
46     $form = parent::form($form, $form_state);
47
48     $entity_type = $this->entity;
49     if ($this->operation == 'add') {
50       $form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
51     }
52     else {
53       $form['#title'] = $this->t(
54         'Edit %label {{ entity_type_label|lower }} type',
55         ['%label' => $entity_type->label()]
56       );
57     }
58
59     $form['label'] = [
60       '#title' => $this->t('Label'),
61       '#type' => 'textfield',
62       '#default_value' => $entity_type->label(),
63       '#description' => $this->t('The human-readable name of this {{ entity_type_label|lower }} type.'),
64       '#required' => TRUE,
65       '#size' => 30,
66     ];
67
68     $form['id'] = [
69       '#type' => 'machine_name',
70       '#default_value' => $entity_type->id(),
71       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
72       '#machine_name' => [
73         'exists' => ['Drupal\{{ machine_name }}\Entity\{{ class_prefix }}Type', 'load'],
74         'source' => ['label'],
75       ],
76       '#description' => $this->t('A unique machine-readable name for this {{ entity_type_label|lower }} type. It must only contain lowercase letters, numbers, and underscores.'),
77     ];
78
79     return $this->protectBundleIdElement($form);
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   protected function actions(array $form, FormStateInterface $form_state) {
86     $actions = parent::actions($form, $form_state);
87     $actions['submit']['#value'] = $this->t('Save {{ entity_type_label|lower }} type');
88     $actions['delete']['#value'] = $this->t('Delete {{ entity_type_label|lower }} type');
89     return $actions;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function save(array $form, FormStateInterface $form_state) {
96     $entity_type = $this->entity;
97
98     $entity_type->set('id', trim($entity_type->id()));
99     $entity_type->set('label', trim($entity_type->label()));
100
101     $status = $entity_type->save();
102
103     $t_args = ['%name' => $entity_type->label()];
104
105     if ($status == SAVED_UPDATED) {
106       $message = 'The {{ entity_type_label|lower }} type %name has been updated.';
107     }
108     elseif ($status == SAVED_NEW) {
109       $message = 'The {{ entity_type_label|lower }} type %name has been added.';
110     }
111     drupal_set_message($this->t($message, $t_args));
112
113     $this->entityManager->clearCachedFieldDefinitions();
114     $form_state->setRedirectUrl($entity_type->urlInfo('collection'));
115   }
116
117 }