Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / templates / module / src / Form / entity.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{ module }}\Form\{{ entity_class }}Form.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Form;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Entity\EntityForm;
13 use Drupal\Core\Form\FormStateInterface;
14 {% endblock %}
15
16 {% block class_declaration %}
17 /**
18  * Class {{ entity_class }}Form.
19  */
20 class {{ entity_class }}Form extends EntityForm {% endblock %}
21 {% block class_methods %}
22   /**
23    * {@inheritdoc}
24    */
25   public function form(array $form, FormStateInterface $form_state) {
26     $form = parent::form($form, $form_state);
27
28     ${{ entity_name | machine_name }} = $this->entity;
29     $form['label'] = [
30       '#type' => 'textfield',
31       '#title' => $this->t('Label'),
32       '#maxlength' => 255,
33       '#default_value' => ${{ entity_name | machine_name }}->label(),
34       '#description' => $this->t("Label for the {{ label }}."),
35       '#required' => TRUE,
36     ];
37
38     $form['id'] = [
39       '#type' => 'machine_name',
40       '#default_value' => ${{ entity_name | machine_name }}->id(),
41       '#machine_name' => [
42         'exists' => '\Drupal\{{ module }}\Entity\{{ entity_class }}::load',
43       ],
44       '#disabled' => !${{ entity_name | machine_name }}->isNew(),
45     ];
46
47     /* You will need additional form elements for your custom properties. */
48
49     return $form;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function save(array $form, FormStateInterface $form_state) {
56     ${{ entity_name | machine_name }} = $this->entity;
57     $status = ${{ entity_name | machine_name }}->save();
58
59     switch ($status) {
60       case SAVED_NEW:
61         drupal_set_message($this->t('Created the %label {{ label }}.', [
62           '%label' => ${{ entity_name | machine_name }}->label(),
63         ]));
64         break;
65
66       default:
67         drupal_set_message($this->t('Saved the %label {{ label }}.', [
68           '%label' => ${{ entity_name | machine_name }}->label(),
69         ]));
70     }
71     $form_state->setRedirectUrl(${{ entity_name | machine_name }}->toUrl('collection'));
72   }
73 {% endblock %}