0b368585b737675d906bd497a91e83fbe1b9cdd8
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowStateAddForm.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
9 /**
10  * Class WorkflowStateAddForm.
11  */
12 class WorkflowStateAddForm extends EntityForm {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormId() {
18     return 'workflow_state_add_form';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function form(array $form, FormStateInterface $form_state) {
25     $form = parent::form($form, $form_state);
26
27     /* @var \Drupal\workflows\WorkflowInterface $workflow */
28     $workflow = $this->getEntity();
29     $form['label'] = [
30       '#type' => 'textfield',
31       '#title' => $this->t('Label'),
32       '#maxlength' => 255,
33       '#default_value' => '',
34       '#description' => $this->t('Label for the state.'),
35       '#required' => TRUE,
36     ];
37
38     $form['id'] = [
39       '#type' => 'machine_name',
40       '#machine_name' => [
41         'exists' => [$this, 'exists'],
42       ],
43     ];
44
45     // Add additional form fields from the workflow type plugin.
46     $form['type_settings'] = [
47       $workflow->get('type') => $workflow->getTypePlugin()->buildStateConfigurationForm($form_state, $workflow),
48       '#tree' => TRUE,
49     ];
50
51     return $form;
52   }
53
54   /**
55    * Determines if the workflow state already exists.
56    *
57    * @param string $state_id
58    *   The workflow state ID.
59    *
60    * @return bool
61    *   TRUE if the workflow state exists, FALSE otherwise.
62    */
63   public function exists($state_id) {
64     /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
65     $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
66     return $original_workflow->hasState($state_id);
67   }
68
69   /**
70    * Copies top-level form values to entity properties
71    *
72    * This form can only change values for a state, which is part of workflow.
73    *
74    * @param \Drupal\Core\Entity\EntityInterface $entity
75    *   The entity the current form should operate upon.
76    * @param array $form
77    *   A nested array of form elements comprising the form.
78    * @param \Drupal\Core\Form\FormStateInterface $form_state
79    *   The current state of the form.
80    */
81   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
82     /** @var \Drupal\workflows\WorkflowInterface $entity */
83     $values = $form_state->getValues();
84
85     // This is fired twice so we have to check that the entity does not already
86     // have the state.
87     if (!$entity->hasState($values['id'])) {
88       $entity->addState($values['id'], $values['label']);
89       if (isset($values['type_settings'])) {
90         $configuration = $entity->getTypePlugin()->getConfiguration();
91         $configuration['states'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
92         $entity->set('type_settings', $configuration);
93       }
94     }
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function save(array $form, FormStateInterface $form_state) {
101     /** @var \Drupal\workflows\WorkflowInterface $workflow */
102     $workflow = $this->entity;
103     $workflow->save();
104     drupal_set_message($this->t('Created %label state.', [
105       '%label' => $workflow->getState($form_state->getValue('id'))->label(),
106     ]));
107     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function actions(array $form, FormStateInterface $form_state) {
114     $actions['submit'] = [
115       '#type' => 'submit',
116       '#value' => $this->t('Save'),
117       '#submit' => ['::submitForm', '::save'],
118     ];
119     return $actions;
120   }
121
122 }