Version 1
[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->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->hasState($workflow_state)) {
74       throw new NotFoundHttpException();
75     }
76     $this->workflow = $workflow;
77     $this->stateId = $workflow_state;
78     return parent::buildForm($form, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitForm(array &$form, FormStateInterface $form_state) {
85
86     $workflow_label = $this->workflow->getState($this->stateId)->label();
87     $this->workflow
88       ->deleteState($this->stateId)
89       ->save();
90
91     drupal_set_message($this->t(
92       'State %label deleted.',
93       ['%label' => $workflow_label]
94     ));
95
96     $form_state->setRedirectUrl($this->getCancelUrl());
97   }
98
99 }