5bbabae6f7af3b75eda191aab7594e7b33ddc515
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowTransitionEditForm.php
1 <?php
2
3 namespace Drupal\workflows\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Drupal\workflows\State;
10
11 /**
12  * Class WorkflowTransitionEditForm.
13  */
14 class WorkflowTransitionEditForm extends EntityForm {
15
16   /**
17    * The ID of the transition that is being edited.
18    *
19    * @var string
20    */
21   protected $transitionId;
22
23   /**
24    * {@inheritdoc}
25    */
26   public function getFormId() {
27     return 'workflow_transition_edit_form';
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function buildForm(array $form, FormStateInterface $form_state, $workflow_transition = NULL) {
34     $this->transitionId = $workflow_transition;
35     return parent::buildForm($form, $form_state);
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function form(array $form, FormStateInterface $form_state) {
42     $form = parent::form($form, $form_state);
43
44     /* @var \Drupal\workflows\WorkflowInterface $workflow */
45     $workflow = $this->getEntity();
46     $transition = $workflow->getTransition($this->transitionId);
47     $form['label'] = [
48       '#type' => 'textfield',
49       '#title' => $this->t('Label'),
50       '#maxlength' => 255,
51       '#default_value' => $transition->label(),
52       '#description' => $this->t('Label for the transition.'),
53       '#required' => TRUE,
54     ];
55
56     $form['id'] = [
57       '#type' => 'value',
58       '#value' => $this->transitionId,
59     ];
60
61     // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
62     //   only valid transitions are selectable.
63     $states = array_map([State::class, 'labelCallback'], $workflow->getStates());
64     $form['from'] = [
65       '#type' => 'checkboxes',
66       '#title' => $this->t('From'),
67       '#required' => TRUE,
68       '#default_value' => array_keys($transition->from()),
69       '#options' => $states,
70     ];
71     $form['to'] = [
72       '#type' => 'radios',
73       '#title' => $this->t('To'),
74       '#required' => TRUE,
75       '#default_value' => $transition->to()->id(),
76       '#options' => $states,
77       '#disabled' => TRUE,
78     ];
79
80     // Add additional form fields from the workflow type plugin.
81     $form['type_settings'] = [
82       $workflow->get('type') => $workflow->getTypePlugin()->buildTransitionConfigurationForm($form_state, $workflow, $transition),
83       '#tree' => TRUE,
84     ];
85
86     return $form;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function validateForm(array &$form, FormStateInterface $form_state) {
93     /** @var \Drupal\workflows\WorkflowInterface $workflow */
94     $workflow = $this->getEntity();
95     $values = $form_state->getValues();
96     foreach (array_filter($values['from']) as $from_state_id) {
97       if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
98         $transition = $workflow->getTransitionFromStateToState($from_state_id, $values['to']);
99         if ($transition->id() !== $values['id']) {
100           $form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
101             '%from' => $workflow->getState($from_state_id)->label(),
102             '%to' => $workflow->getState($values['to'])->label(),
103           ]));
104         }
105       }
106     }
107   }
108
109   /**
110    * Copies top-level form values to entity properties
111    *
112    * This form can only change values for a state, which is part of workflow.
113    *
114    * @param \Drupal\Core\Entity\EntityInterface $entity
115    *   The entity the current form should operate upon.
116    * @param array $form
117    *   A nested array of form elements comprising the form.
118    * @param \Drupal\Core\Form\FormStateInterface $form_state
119    *   The current state of the form.
120    */
121   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
122     if (!$form_state->isValidationComplete()) {
123       // Only do something once form validation is complete.
124       return;
125     }
126     /** @var \Drupal\workflows\WorkflowInterface $entity */
127     $values = $form_state->getValues();
128     $form_state->set('created_transition', FALSE);
129     $entity->setTransitionLabel($values['id'], $values['label']);
130     $entity->setTransitionFromStates($values['id'], array_filter($values['from']));
131     if (isset($values['type_settings'])) {
132       $configuration = $entity->getTypePlugin()->getConfiguration();
133       $configuration['transitions'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
134       $entity->set('type_settings', $configuration);
135     }
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function save(array $form, FormStateInterface $form_state) {
142     /** @var \Drupal\workflows\WorkflowInterface $workflow */
143     $workflow = $this->entity;
144     $workflow->save();
145     drupal_set_message($this->t('Saved %label transition.', [
146       '%label' => $workflow->getTransition($this->transitionId)->label(),
147     ]));
148     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   protected function actions(array $form, FormStateInterface $form_state) {
155     $actions['submit'] = [
156       '#type' => 'submit',
157       '#value' => $this->t('Save'),
158       '#submit' => ['::submitForm', '::save'],
159     ];
160
161     $actions['delete'] = [
162       '#type' => 'link',
163       '#title' => $this->t('Delete'),
164       // Deleting a transition is editing a workflow.
165       '#access' => $this->entity->access('edit'),
166       '#attributes' => [
167         'class' => ['button', 'button--danger'],
168       ],
169       '#url' => Url::fromRoute('entity.workflow.delete_transition_form', [
170         'workflow' => $this->entity->id(),
171         'workflow_transition' => $this->transitionId
172       ])
173     ];
174
175     return $actions;
176   }
177
178 }