fe3a40636d659eed594d8245c2a1a8137e247972
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowTransitionAddForm.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\workflows\State;
9
10 /**
11  * Class WorkflowTransitionAddForm.
12  */
13 class WorkflowTransitionAddForm extends EntityForm {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'workflow_transition_add_form';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function form(array $form, FormStateInterface $form_state) {
26     $form = parent::form($form, $form_state);
27
28     /* @var \Drupal\workflows\WorkflowInterface $workflow */
29     $workflow = $this->getEntity();
30     $form['label'] = [
31       '#type' => 'textfield',
32       '#title' => $this->t('Label'),
33       '#maxlength' => 255,
34       '#default_value' => '',
35       '#description' => $this->t('Label for the transition.'),
36       '#required' => TRUE,
37     ];
38
39     $form['id'] = [
40       '#type' => 'machine_name',
41       '#machine_name' => [
42         'exists' => [$this, 'exists'],
43       ],
44     ];
45
46     // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
47     //   only valid transitions are selectable.
48     $states = array_map([State::class, 'labelCallback'], $workflow->getStates());
49     $form['from'] = [
50       '#type' => 'checkboxes',
51       '#title' => $this->t('From'),
52       '#required' => TRUE,
53       '#default_value' => [],
54       '#options' => $states,
55     ];
56     $form['to'] = [
57       '#type' => 'radios',
58       '#title' => $this->t('To'),
59       '#required' => TRUE,
60       '#default_value' => [],
61       '#options' => $states,
62     ];
63
64     // Add additional form fields from the workflow type plugin.
65     $form['type_settings'] = [
66       $workflow->get('type') => $workflow->getTypePlugin()->buildTransitionConfigurationForm($form_state, $workflow),
67       '#tree' => TRUE,
68     ];
69
70     return $form;
71   }
72
73   /**
74    * Determines if the workflow transition already exists.
75    *
76    * @param string $transition_id
77    *   The workflow transition ID.
78    *
79    * @return bool
80    *   TRUE if the workflow transition exists, FALSE otherwise.
81    */
82   public function exists($transition_id) {
83     /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
84     $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
85     return $original_workflow->hasTransition($transition_id);
86   }
87
88   /**
89    * Copies top-level form values to entity properties
90    *
91    * This form can only change values for a state, which is part of workflow.
92    *
93    * @param \Drupal\Core\Entity\EntityInterface $entity
94    *   The entity the current form should operate upon.
95    * @param array $form
96    *   A nested array of form elements comprising the form.
97    * @param \Drupal\Core\Form\FormStateInterface $form_state
98    *   The current state of the form.
99    */
100   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
101     if (!$form_state->isValidationComplete()) {
102       // Only do something once form validation is complete.
103       return;
104     }
105     /** @var \Drupal\workflows\WorkflowInterface $entity */
106     $values = $form_state->getValues();
107     $entity->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
108     if (isset($values['type_settings'])) {
109       $configuration = $entity->getTypePlugin()->getConfiguration();
110       $configuration['transitions'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
111       $entity->set('type_settings', $configuration);
112     }
113   }
114
115
116   /**
117    * {@inheritdoc}
118    */
119   public function validateForm(array &$form, FormStateInterface $form_state) {
120     /** @var \Drupal\workflows\WorkflowInterface $workflow */
121     $workflow = $this->getEntity();
122     $values = $form_state->getValues();
123     foreach (array_filter($values['from']) as $from_state_id) {
124       if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
125         $form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
126           '%from' => $workflow->getState($from_state_id)->label(),
127           '%to' => $workflow->getState($values['to'])->label(),
128         ]));
129       }
130     }
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function save(array $form, FormStateInterface $form_state) {
137     /** @var \Drupal\workflows\WorkflowInterface $workflow */
138     $workflow = $this->entity;
139     $workflow->save();
140     drupal_set_message($this->t('Created %label transition.', [
141       '%label' => $form_state->getValue('label'),
142     ]));
143     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   protected function actions(array $form, FormStateInterface $form_state) {
150     $actions['submit'] = [
151       '#type' => 'submit',
152       '#value' => $this->t('Save'),
153       '#submit' => ['::submitForm', '::save'],
154     ];
155     return $actions;
156   }
157
158 }