Version 1
[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  * @package Drupal\{{ module }}\Form
21  */
22 class {{ entity_class }}Form extends EntityForm {% endblock %}
23 {% block class_methods %}
24   /**
25    * {@inheritdoc}
26    */
27   public function form(array $form, FormStateInterface $form_state) {
28     $form = parent::form($form, $form_state);
29
30     ${{ entity_name | machine_name }} = $this->entity;
31     $form['label'] = [
32       '#type' => 'textfield',
33       '#title' => $this->t('Label'),
34       '#maxlength' => 255,
35       '#default_value' => ${{ entity_name | machine_name }}->label(),
36       '#description' => $this->t("Label for the {{ label }}."),
37       '#required' => TRUE,
38     ];
39
40     $form['id'] = [
41       '#type' => 'machine_name',
42       '#default_value' => ${{ entity_name | machine_name }}->id(),
43       '#machine_name' => [
44         'exists' => '\Drupal\{{ module }}\Entity\{{ entity_class }}::load',
45       ],
46       '#disabled' => !${{ entity_name | machine_name }}->isNew(),
47     ];
48
49     /* You will need additional form elements for your custom properties. */
50
51     return $form;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function save(array $form, FormStateInterface $form_state) {
58     ${{ entity_name | machine_name }} = $this->entity;
59     $status = ${{ entity_name | machine_name }}->save();
60
61     switch ($status) {
62       case SAVED_NEW:
63         drupal_set_message($this->t('Created the %label {{ label }}.', [
64           '%label' => ${{ entity_name | machine_name }}->label(),
65         ]));
66         break;
67
68       default:
69         drupal_set_message($this->t('Saved the %label {{ label }}.', [
70           '%label' => ${{ entity_name | machine_name }}->label(),
71         ]));
72     }
73     $form_state->setRedirectUrl(${{ entity_name | machine_name }}->toUrl('collection'));
74   }
75 {% endblock %}