Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / ContentEntityConfirmFormBase.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Form\ConfirmFormHelper;
6 use Drupal\Core\Form\ConfirmFormInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Provides a generic base class for an entity-based confirmation form.
11  */
12 abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements ConfirmFormInterface {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getBaseFormId() {
18     return $this->entity->getEntityTypeId() . '_confirm_form';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function getDescription() {
25     return $this->t('This action cannot be undone.');
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function getConfirmText() {
32     return $this->t('Confirm');
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function getCancelText() {
39     return $this->t('Cancel');
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormName() {
46     return 'confirm';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildForm(array $form, FormStateInterface $form_state) {
53     $form = parent::buildForm($form, $form_state);
54
55     $form['#title'] = $this->getQuestion();
56
57     $form['#attributes']['class'][] = 'confirmation';
58     $form['description'] = ['#markup' => $this->getDescription()];
59     $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
60
61     // By default, render the form using theme_confirm_form().
62     if (!isset($form['#theme'])) {
63       $form['#theme'] = 'confirm_form';
64     }
65     return $form;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function form(array $form, FormStateInterface $form_state) {
72     // Do not attach fields to the confirm form.
73     return $form;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   protected function actions(array $form, FormStateInterface $form_state) {
80     return [
81       'submit' => [
82         '#type' => 'submit',
83         '#value' => $this->getConfirmText(),
84         '#submit' => [
85           [$this, 'submitForm'],
86         ],
87       ],
88       'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
89     ];
90   }
91
92   /**
93    * {@inheritdoc}
94    *
95    * The save() method is not used in ContentEntityConfirmFormBase. This
96    * overrides the default implementation that saves the entity.
97    *
98    * Confirmation forms should override submitForm() instead for their logic.
99    */
100   public function save(array $form, FormStateInterface $form_state) {}
101
102   /**
103    * {@inheritdoc}
104    *
105    * The delete() method is not used in ContentEntityConfirmFormBase. This
106    * overrides the default implementation that redirects to the delete-form
107    * confirmation form.
108    *
109    * Confirmation forms should override submitForm() instead for their logic.
110    */
111   public function delete(array $form, FormStateInterface $form_state) {}
112
113   /**
114    * {@inheritdoc}
115    */
116   public function validateForm(array &$form, FormStateInterface $form_state) {
117     // Override the default validation implementation as it is not necessary
118     // nor possible to validate an entity in a confirmation form.
119     return $this->entity;
120   }
121
122 }