Further changes for the Use cases on the live site.
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowEditForm.php
1 <?php
2
3 namespace Drupal\workflows\Form;
4
5 use Drupal\workflows\Entity\Workflow;
6 use Drupal\workflows\State;
7 use Drupal\Core\Entity\EntityForm;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * The form for editing workflows.
14  */
15 class WorkflowEditForm extends EntityForm {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function form(array $form, FormStateInterface $form_state) {
21     $form = parent::form($form, $form_state);
22
23     /* @var \Drupal\workflows\WorkflowInterface $workflow */
24     $workflow = $this->entity;
25     $form['label'] = [
26       '#type' => 'textfield',
27       '#title' => $this->t('Label'),
28       '#maxlength' => 255,
29       '#default_value' => $workflow->label(),
30       '#description' => $this->t('Label for the Workflow.'),
31       '#required' => TRUE,
32     ];
33
34     $form['id'] = [
35       '#type' => 'machine_name',
36       '#default_value' => $workflow->id(),
37       '#machine_name' => [
38         'exists' => [Workflow::class, 'load'],
39       ],
40       '#disabled' => TRUE,
41     ];
42
43     $header = [
44       'state' => $this->t('State'),
45       'weight' => $this->t('Weight'),
46       'operations' => $this->t('Operations')
47     ];
48     $form['states_container'] = [
49       '#type' => 'details',
50       '#title' => $this->t('States'),
51       '#open' => TRUE,
52       '#collapsible' => 'FALSE',
53     ];
54     $form['states_container']['states'] = [
55       '#type' => 'table',
56       '#header' => $header,
57       '#title' => $this->t('States'),
58       '#empty' => $this->t('There are no states yet.'),
59       '#tabledrag' => [
60         [
61           'action' => 'order',
62           'relationship' => 'sibling',
63           'group' => 'state-weight',
64         ],
65       ],
66     ];
67
68     $states = $workflow->getStates();
69
70     // Warn the user if there are no states.
71     if (empty($states)) {
72       drupal_set_message(
73         $this->t(
74           'This workflow has no states and will be disabled until there is at least one, <a href=":add-state">add a new state.</a>',
75           [':add-state' => $workflow->toUrl('add-state-form')->toString()]
76         ),
77         'warning'
78       );
79     }
80
81     foreach ($states as $state) {
82       $links = [
83         'edit' => [
84           'title' => $this->t('Edit'),
85           'url' => Url::fromRoute('entity.workflow.edit_state_form', ['workflow' => $workflow->id(), 'workflow_state' => $state->id()]),
86           'attributes' => ['aria-label' => $this->t('Edit @state state', ['@state' => $state->label()])],
87         ]
88       ];
89       if ($this->entity->access('delete-state:' . $state->id())) {
90         $links['delete'] = [
91           'title' => t('Delete'),
92           'url' => Url::fromRoute('entity.workflow.delete_state_form', [
93             'workflow' => $workflow->id(),
94             'workflow_state' => $state->id()
95           ]),
96           'attributes' => ['aria-label' => $this->t('Delete @state state', ['@state' => $state->label()])],
97         ];
98       }
99       $form['states_container']['states'][$state->id()] = [
100         '#attributes' => ['class' => ['draggable']],
101         'state' => ['#markup' => $state->label()],
102         '#weight' => $state->weight(),
103         'weight' => [
104           '#type' => 'weight',
105           '#title' => t('Weight for @title', ['@title' => $state->label()]),
106           '#title_display' => 'invisible',
107           '#default_value' => $state->weight(),
108           '#attributes' => ['class' => ['state-weight']],
109         ],
110         'operations' => [
111           '#type' => 'operations',
112           '#links' => $links,
113         ],
114       ];
115     }
116     $form['states_container']['state_add'] = [
117       '#markup' => $workflow->toLink($this->t('Add a new state'), 'add-state-form')->toString(),
118     ];
119
120     $header = [
121       'label' => $this->t('Label'),
122       'weight' => $this->t('Weight'),
123       'from' => $this->t('From'),
124       'to' => $this->t('To'),
125       'operations' => $this->t('Operations')
126     ];
127     $form['transitions_container'] = [
128       '#type' => 'details',
129       '#title' => $this->t('Transitions'),
130       '#open' => TRUE,
131     ];
132     $form['transitions_container']['transitions'] = [
133       '#type' => 'table',
134       '#header' => $header,
135       '#title' => $this->t('Transitions'),
136       '#empty' => $this->t('There are no transitions yet.'),
137       '#tabledrag' => [
138         [
139           'action' => 'order',
140           'relationship' => 'sibling',
141           'group' => 'transition-weight',
142         ],
143       ],
144     ];
145     foreach ($workflow->getTransitions() as $transition) {
146       $links['edit'] = [
147         'title' => $this->t('Edit'),
148         'url' => Url::fromRoute('entity.workflow.edit_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
149         'attributes' => ['aria-label' => $this->t('Edit \'@transition\' transition', ['@transition' => $transition->label()])],
150       ];
151       $links['delete'] = [
152         'title' => t('Delete'),
153         'url' => Url::fromRoute('entity.workflow.delete_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
154         'attributes' => ['aria-label' => $this->t('Delete \'@transition\' transition', ['@transition' => $transition->label()])],
155       ];
156       $form['transitions_container']['transitions'][$transition->id()] = [
157         '#attributes' => ['class' => ['draggable']],
158         'label' => ['#markup' => $transition->label()],
159         '#weight' => $transition->weight(),
160         'weight' => [
161           '#type' => 'weight',
162           '#title' => t('Weight for @title', ['@title' => $transition->label()]),
163           '#title_display' => 'invisible',
164           '#default_value' => $transition->weight(),
165           '#attributes' => ['class' => ['transition-weight']],
166         ],
167         'from' => [
168           '#theme' => 'item_list',
169           '#items' => array_map([State::class, 'labelCallback'], $transition->from()),
170           '#context' => ['list_style' => 'comma-list'],
171         ],
172         'to' => ['#markup' => $transition->to()->label()],
173         'operations' => [
174           '#type' => 'operations',
175           '#links' => $links,
176         ],
177       ];
178     }
179     $form['transitions_container']['transition_add'] = [
180       '#markup' => $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(),
181     ];
182
183     return $form;
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public function save(array $form, FormStateInterface $form_state) {
190     /* @var \Drupal\workflows\WorkflowInterface $workflow */
191     $workflow = $this->entity;
192     $workflow->save();
193     drupal_set_message($this->t('Saved the %label Workflow.', ['%label' => $workflow->label()]));
194     $form_state->setRedirectUrl($workflow->toUrl('collection'));
195   }
196
197   /**
198    * {@inheritdoc}
199    */
200   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
201     // This form can only set the workflow's ID, label and the weights for each
202     // state.
203     /** @var \Drupal\workflows\WorkflowInterface $entity */
204     $values = $form_state->getValues();
205     $entity->set('label', $values['label']);
206     $entity->set('id', $values['id']);
207     foreach ($values['states'] as $state_id => $state_values) {
208       $entity->setStateWeight($state_id, $state_values['weight']);
209     }
210     foreach ($values['transitions'] as $transition_id => $transition_values) {
211       $entity->setTransitionWeight($transition_id, $transition_values['weight']);
212     }
213   }
214
215 }