Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowStateDeleteForm.php
1 <?php
2
3 namespace Drupal\workflows\Form;
4
5 use Drupal\workflows\WorkflowInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10 /**
11  * Builds the form to delete states from Workflow entities.
12  */
13 class WorkflowStateDeleteForm extends ConfirmFormBase {
14
15   /**
16    * The workflow entity the state being deleted belongs to.
17    *
18    * @var \Drupal\workflows\WorkflowInterface
19    */
20   protected $workflow;
21
22   /**
23    * The state being deleted.
24    *
25    * @var string
26    */
27   protected $stateId;
28
29   /**
30    * {@inheritdoc}
31    */
32   public function getFormId() {
33     return 'workflow_state_delete_form';
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function getQuestion() {
40     return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getTypePlugin()->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getCancelUrl() {
47     return $this->workflow->toUrl();
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getConfirmText() {
54     return $this->t('Delete');
55   }
56
57   /**
58    * Form constructor.
59    *
60    * @param array $form
61    *   An associative array containing the structure of the form.
62    * @param \Drupal\Core\Form\FormStateInterface $form_state
63    *   The current state of the form.
64    * @param \Drupal\workflows\WorkflowInterface $workflow
65    *   The workflow entity being edited.
66    * @param string|null $workflow_state
67    *   The workflow state being deleted.
68    *
69    * @return array
70    *   The form structure.
71    */
72   public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_state = NULL) {
73     if (!$workflow->getTypePlugin()->hasState($workflow_state)) {
74       throw new NotFoundHttpException();
75     }
76     $this->workflow = $workflow;
77     $this->stateId = $workflow_state;
78
79     if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getTypePlugin()->getState($this->stateId))) {
80       $form['#title'] = $this->getQuestion();
81       $form['description'] = ['#markup' => $this->t('This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.')];
82       return $form;
83     }
84
85     return parent::buildForm($form, $form_state);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function submitForm(array &$form, FormStateInterface $form_state) {
92
93     $workflow_label = $this->workflow->getTypePlugin()->getState($this->stateId)->label();
94     $this->workflow
95       ->getTypePlugin()
96       ->deleteState($this->stateId);
97     $this->workflow->save();
98
99     drupal_set_message($this->t(
100       'State %label deleted.',
101       ['%label' => $workflow_label]
102     ));
103
104     $form_state->setRedirectUrl($this->getCancelUrl());
105   }
106
107 }