1fee5d41579151ae1f2f3c29c7b60723e7bcfcff
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowTransitionDeleteForm.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 transitions from Workflow entities.
12  *
13  * @internal
14  */
15 class WorkflowTransitionDeleteForm extends ConfirmFormBase {
16
17   /**
18    * The workflow entity the transition being deleted belongs to.
19    *
20    * @var \Drupal\workflows\WorkflowInterface
21    */
22   protected $workflow;
23
24   /**
25    * The workflow transition being deleted.
26    *
27    * @var \Drupal\workflows\TransitionInterface
28    */
29   protected $transition;
30
31   /**
32    * The transition being deleted.
33    *
34    * @var string
35    */
36   protected $transitionId;
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getFormId() {
42     return 'workflow_transition_delete_form';
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getQuestion() {
49     return $this->t('Are you sure you want to delete %transition from %workflow?', ['%transition' => $this->transition->label(), '%workflow' => $this->workflow->label()]);
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getCancelUrl() {
56     return $this->workflow->toUrl();
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getConfirmText() {
63     return $this->t('Delete');
64   }
65
66   /**
67    * Form constructor.
68    *
69    * @param array $form
70    *   An associative array containing the structure of the form.
71    * @param \Drupal\Core\Form\FormStateInterface $form_state
72    *   The current state of the form.
73    * @param \Drupal\workflows\WorkflowInterface $workflow
74    *   The workflow entity being edited.
75    * @param string|null $workflow_transition
76    *   The workflow transition being deleted.
77    *
78    * @return array
79    *   The form structure.
80    */
81   public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_transition = NULL) {
82     try {
83       $this->transition = $workflow->getTypePlugin()->getTransition($workflow_transition);
84     }
85     catch (\InvalidArgumentException $e) {
86       throw new NotFoundHttpException();
87     }
88     $this->workflow = $workflow;
89     return parent::buildForm($form, $form_state);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function submitForm(array &$form, FormStateInterface $form_state) {
96     $this->workflow
97       ->getTypePlugin()
98       ->deleteTransition($this->transition->id());
99     $this->workflow->save();
100
101     $this->messenger()->addStatus($this->t('%transition transition deleted.', ['%transition' => $this->transition->label()]));
102     $form_state->setRedirectUrl($this->getCancelUrl());
103   }
104
105 }