Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowStateEditForm.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\Form\SubformState;
9 use Drupal\Core\Plugin\PluginFormFactoryInterface;
10 use Drupal\Core\Url;
11 use Drupal\workflows\StateInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Class WorkflowStateEditForm.
16  */
17 class WorkflowStateEditForm extends EntityForm {
18
19   /**
20    * The ID of the state that is being edited.
21    *
22    * @var string
23    */
24   protected $stateId;
25
26   /**
27    * The plugin form factory.
28    *
29    * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
30    */
31   protected $pluginFormFactory;
32
33   /**
34    * Creates an instance of WorkflowStateEditForm.
35    *
36    * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
37    *   The plugin form factory.
38    */
39   public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
40     $this->pluginFormFactory = $pluginFormFactory;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('plugin_form.factory')
49     );
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getFormId() {
56     return 'workflow_state_edit_form';
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function buildForm(array $form, FormStateInterface $form_state, $workflow_state = NULL) {
63     $this->stateId = $workflow_state;
64     return parent::buildForm($form, $form_state);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function form(array $form, FormStateInterface $form_state) {
71     $form = parent::form($form, $form_state);
72
73     /* @var \Drupal\workflows\WorkflowInterface $workflow */
74     $workflow = $this->getEntity();
75     $workflow_type = $workflow->getTypePlugin();
76     $state = $workflow->getTypePlugin()->getState($this->stateId);
77
78     $form['label'] = [
79       '#type' => 'textfield',
80       '#title' => $this->t('Label'),
81       '#maxlength' => 255,
82       '#default_value' => $state->label(),
83       '#description' => $this->t('Label for the state.'),
84       '#required' => TRUE,
85     ];
86
87     $form['id'] = [
88       '#type' => 'machine_name',
89       '#default_value' => $this->stateId,
90       '#machine_name' => [
91         'exists' => [$this, 'exists'],
92       ],
93       '#disabled' => TRUE,
94     ];
95
96     // Add additional form fields from the workflow type plugin.
97     if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
98       $form['type_settings'] = [
99         '#tree' => TRUE,
100       ];
101       $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
102       $subform_state->set('state', $state);
103       $form['type_settings'] += $this->pluginFormFactory
104         ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
105         ->buildConfigurationForm($form['type_settings'], $subform_state);
106     }
107
108     $header = [
109       'label' => $this->t('Transition'),
110       'state' => $this->t('To'),
111       'operations' => $this->t('Operations'),
112     ];
113     $form['transitions'] = [
114       '#type' => 'table',
115       '#header' => $header,
116       '#empty' => $this->t('There are no transitions to or from this state yet.'),
117     ];
118     foreach ($state->getTransitions() as $transition) {
119       $links['edit'] = [
120         'title' => $this->t('Edit'),
121         'url' => Url::fromRoute('entity.workflow.edit_transition_form', [
122           'workflow' => $workflow->id(),
123           'workflow_transition' => $transition->id()
124         ]),
125       ];
126       $links['delete'] = [
127         'title' => t('Delete'),
128         'url' => Url::fromRoute('entity.workflow.delete_transition_form', [
129           'workflow' => $workflow->id(),
130           'workflow_transition' => $transition->id()
131         ]),
132       ];
133       $form['transitions'][$transition->id()] = [
134         'label' => [
135           '#markup' => $transition->label(),
136         ],
137         'state' => [
138           '#markup' => $transition->to()->label(),
139         ],
140         'operations' => [
141           '#type' => 'operations',
142           '#links' => $links,
143         ],
144       ];
145     }
146
147     return $form;
148   }
149
150   /**
151    * Copies top-level form values to entity properties
152    *
153    * This form can only change values for a state, which is part of workflow.
154    *
155    * @param \Drupal\Core\Entity\EntityInterface $entity
156    *   The entity the current form should operate upon.
157    * @param array $form
158    *   A nested array of form elements comprising the form.
159    * @param \Drupal\Core\Form\FormStateInterface $form_state
160    *   The current state of the form.
161    */
162   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
163     /** @var \Drupal\workflows\WorkflowInterface $entity */
164     $values = $form_state->getValues();
165     $entity->getTypePlugin()->setStateLabel($values['id'], $values['label']);
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function validateForm(array &$form, FormStateInterface $form_state) {
172     parent::validateForm($form, $form_state);
173     /** @var \Drupal\workflows\WorkflowTypeInterface $workflow_type */
174     $workflow = $this->entity;
175     $workflow_type = $workflow->getTypePlugin();
176
177     if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
178       $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
179       $subform_state->set('state', $workflow_type->getState($this->stateId));
180       $this->pluginFormFactory
181         ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
182         ->validateConfigurationForm($form['type_settings'], $subform_state);
183     }
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_type = $workflow->getTypePlugin();
193
194     if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
195       $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
196       $subform_state->set('state', $workflow_type->getState($this->stateId));
197       $this->pluginFormFactory
198         ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
199         ->submitConfigurationForm($form['type_settings'], $subform_state);
200     }
201
202     $workflow->save();
203     drupal_set_message($this->t('Saved %label state.', [
204       '%label' => $workflow->getTypePlugin()->getState($this->stateId)->label(),
205     ]));
206     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
207   }
208
209   /**
210    * {@inheritdoc}
211    */
212   protected function actions(array $form, FormStateInterface $form_state) {
213     $actions['submit'] = [
214       '#type' => 'submit',
215       '#value' => $this->t('Save'),
216       '#submit' => ['::submitForm', '::save'],
217     ];
218
219     $actions['delete'] = [
220       '#type' => 'link',
221       '#title' => $this->t('Delete'),
222       '#access' => $this->entity->access('delete-state:' . $this->stateId),
223       '#attributes' => [
224         'class' => ['button', 'button--danger'],
225       ],
226       '#url' => Url::fromRoute('entity.workflow.delete_state_form', [
227         'workflow' => $this->entity->id(),
228         'workflow_state' => $this->stateId
229       ])
230     ];
231
232     return $actions;
233   }
234
235 }