Version 1
[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\Url;
9
10 /**
11  * Class WorkflowStateEditForm.
12  */
13 class WorkflowStateEditForm extends EntityForm {
14
15   /**
16    * The ID of the state that is being edited.
17    *
18    * @var string
19    */
20   protected $stateId;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getFormId() {
26     return 'workflow_state_edit_form';
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function buildForm(array $form, FormStateInterface $form_state, $workflow_state = NULL) {
33     $this->stateId = $workflow_state;
34     return parent::buildForm($form, $form_state);
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function form(array $form, FormStateInterface $form_state) {
41     $form = parent::form($form, $form_state);
42
43     /* @var \Drupal\workflows\WorkflowInterface $workflow */
44     $workflow = $this->getEntity();
45     $state = $workflow->getState($this->stateId);
46     $form['label'] = [
47       '#type' => 'textfield',
48       '#title' => $this->t('Label'),
49       '#maxlength' => 255,
50       '#default_value' => $state->label(),
51       '#description' => $this->t('Label for the state.'),
52       '#required' => TRUE,
53     ];
54
55     $form['id'] = [
56       '#type' => 'machine_name',
57       '#default_value' => $this->stateId,
58       '#machine_name' => [
59         'exists' => [$this, 'exists'],
60       ],
61       '#disabled' => TRUE,
62     ];
63
64     // Add additional form fields from the workflow type plugin.
65     $form['type_settings'] = [
66       $workflow->get('type') => $workflow->getTypePlugin()->buildStateConfigurationForm($form_state, $workflow, $state),
67       '#tree' => TRUE,
68     ];
69
70     $header = [
71       'label' => $this->t('Transition'),
72       'state' => $this->t('To'),
73       'operations' => $this->t('Operations'),
74     ];
75     $form['transitions'] = [
76       '#type' => 'table',
77       '#header' => $header,
78       '#empty' => $this->t('There are no transitions to or from this state yet.'),
79     ];
80     foreach ($state->getTransitions() as $transition) {
81       $links['edit'] = [
82         'title' => $this->t('Edit'),
83         'url' => Url::fromRoute('entity.workflow.edit_transition_form', [
84           'workflow' => $workflow->id(),
85           'workflow_transition' => $transition->id()
86         ]),
87       ];
88       $links['delete'] = [
89         'title' => t('Delete'),
90         'url' => Url::fromRoute('entity.workflow.delete_transition_form', [
91           'workflow' => $workflow->id(),
92           'workflow_transition' => $transition->id()
93         ]),
94       ];
95       $form['transitions'][$transition->id()] = [
96         'label' => [
97           '#markup' => $transition->label(),
98         ],
99         'state' => [
100           '#markup' => $transition->to()->label(),
101         ],
102         'operations' => [
103           '#type' => 'operations',
104           '#links' => $links,
105         ],
106       ];
107     }
108
109     return $form;
110   }
111
112   /**
113    * Copies top-level form values to entity properties
114    *
115    * This form can only change values for a state, which is part of workflow.
116    *
117    * @param \Drupal\Core\Entity\EntityInterface $entity
118    *   The entity the current form should operate upon.
119    * @param array $form
120    *   A nested array of form elements comprising the form.
121    * @param \Drupal\Core\Form\FormStateInterface $form_state
122    *   The current state of the form.
123    */
124   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
125     /** @var \Drupal\workflows\WorkflowInterface $entity */
126     $values = $form_state->getValues();
127     $entity->setStateLabel($values['id'], $values['label']);
128     if (isset($values['type_settings'])) {
129       $configuration = $entity->getTypePlugin()->getConfiguration();
130       $configuration['states'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
131       $entity->set('type_settings', $configuration);
132     }
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function save(array $form, FormStateInterface $form_state) {
139     /** @var \Drupal\workflows\WorkflowInterface $workflow */
140     $workflow = $this->entity;
141     $workflow->save();
142     drupal_set_message($this->t('Saved %label state.', [
143       '%label' => $workflow->getState($this->stateId)->label(),
144     ]));
145     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   protected function actions(array $form, FormStateInterface $form_state) {
152     $actions['submit'] = [
153       '#type' => 'submit',
154       '#value' => $this->t('Save'),
155       '#submit' => ['::submitForm', '::save'],
156     ];
157
158     $actions['delete'] = [
159       '#type' => 'link',
160       '#title' => $this->t('Delete'),
161       '#access' => $this->entity->access('delete-state:' . $this->stateId),
162       '#attributes' => [
163         'class' => ['button', 'button--danger'],
164       ],
165       '#url' => Url::fromRoute('entity.workflow.delete_state_form', [
166         'workflow' => $this->entity->id(),
167         'workflow_state' => $this->stateId
168       ])
169     ];
170
171     return $actions;
172   }
173
174 }