3 namespace Drupal\workflows\Form;
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;
13 * The form for editing workflows.
15 class WorkflowEditForm extends EntityForm {
20 public function form(array $form, FormStateInterface $form_state) {
21 $form = parent::form($form, $form_state);
23 /* @var \Drupal\workflows\WorkflowInterface $workflow */
24 $workflow = $this->entity;
26 '#type' => 'textfield',
27 '#title' => $this->t('Label'),
29 '#default_value' => $workflow->label(),
30 '#description' => $this->t('Label for the Workflow.'),
35 '#type' => 'machine_name',
36 '#default_value' => $workflow->id(),
38 'exists' => [Workflow::class, 'load'],
44 'state' => $this->t('State'),
45 'weight' => $this->t('Weight'),
46 'operations' => $this->t('Operations')
48 $form['states_container'] = [
50 '#title' => $this->t('States'),
52 '#collapsible' => 'FALSE',
54 $form['states_container']['states'] = [
57 '#title' => $this->t('States'),
58 '#empty' => $this->t('There are no states yet.'),
62 'relationship' => 'sibling',
63 'group' => 'state-weight',
68 $states = $workflow->getStates();
70 // Warn the user if there are no states.
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()]
81 foreach ($states as $state) {
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()])],
89 if ($this->entity->access('delete-state:' . $state->id())) {
91 'title' => t('Delete'),
92 'url' => Url::fromRoute('entity.workflow.delete_state_form', [
93 'workflow' => $workflow->id(),
94 'workflow_state' => $state->id()
96 'attributes' => ['aria-label' => $this->t('Delete @state state', ['@state' => $state->label()])],
99 $form['states_container']['states'][$state->id()] = [
100 '#attributes' => ['class' => ['draggable']],
101 'state' => ['#markup' => $state->label()],
102 '#weight' => $state->weight(),
105 '#title' => t('Weight for @title', ['@title' => $state->label()]),
106 '#title_display' => 'invisible',
107 '#default_value' => $state->weight(),
108 '#attributes' => ['class' => ['state-weight']],
111 '#type' => 'operations',
116 $form['states_container']['state_add'] = [
117 '#markup' => $workflow->toLink($this->t('Add a new state'), 'add-state-form')->toString(),
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')
127 $form['transitions_container'] = [
128 '#type' => 'details',
129 '#title' => $this->t('Transitions'),
132 $form['transitions_container']['transitions'] = [
134 '#header' => $header,
135 '#title' => $this->t('Transitions'),
136 '#empty' => $this->t('There are no transitions yet.'),
140 'relationship' => 'sibling',
141 'group' => 'transition-weight',
145 foreach ($workflow->getTransitions() as $transition) {
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()])],
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()])],
156 $form['transitions_container']['transitions'][$transition->id()] = [
157 '#attributes' => ['class' => ['draggable']],
158 'label' => ['#markup' => $transition->label()],
159 '#weight' => $transition->weight(),
162 '#title' => t('Weight for @title', ['@title' => $transition->label()]),
163 '#title_display' => 'invisible',
164 '#default_value' => $transition->weight(),
165 '#attributes' => ['class' => ['transition-weight']],
168 '#theme' => 'item_list',
169 '#items' => array_map([State::class, 'labelCallback'], $transition->from()),
170 '#context' => ['list_style' => 'comma-list'],
172 'to' => ['#markup' => $transition->to()->label()],
174 '#type' => 'operations',
179 $form['transitions_container']['transition_add'] = [
180 '#markup' => $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(),
189 public function save(array $form, FormStateInterface $form_state) {
190 /* @var \Drupal\workflows\WorkflowInterface $workflow */
191 $workflow = $this->entity;
193 drupal_set_message($this->t('Saved the %label Workflow.', ['%label' => $workflow->label()]));
194 $form_state->setRedirectUrl($workflow->toUrl('collection'));
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
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']);
210 foreach ($values['transitions'] as $transition_id => $transition_values) {
211 $entity->setTransitionWeight($transition_id, $transition_values['weight']);