3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\workflows\State;
12 * Class WorkflowTransitionEditForm.
14 class WorkflowTransitionEditForm extends EntityForm {
17 * The ID of the transition that is being edited.
21 protected $transitionId;
26 public function getFormId() {
27 return 'workflow_transition_edit_form';
33 public function buildForm(array $form, FormStateInterface $form_state, $workflow_transition = NULL) {
34 $this->transitionId = $workflow_transition;
35 return parent::buildForm($form, $form_state);
41 public function form(array $form, FormStateInterface $form_state) {
42 $form = parent::form($form, $form_state);
44 /* @var \Drupal\workflows\WorkflowInterface $workflow */
45 $workflow = $this->getEntity();
46 $transition = $workflow->getTransition($this->transitionId);
48 '#type' => 'textfield',
49 '#title' => $this->t('Label'),
51 '#default_value' => $transition->label(),
52 '#description' => $this->t('Label for the transition.'),
58 '#value' => $this->transitionId,
61 // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
62 // only valid transitions are selectable.
63 $states = array_map([State::class, 'labelCallback'], $workflow->getStates());
65 '#type' => 'checkboxes',
66 '#title' => $this->t('From'),
68 '#default_value' => array_keys($transition->from()),
69 '#options' => $states,
73 '#title' => $this->t('To'),
75 '#default_value' => $transition->to()->id(),
76 '#options' => $states,
80 // Add additional form fields from the workflow type plugin.
81 $form['type_settings'] = [
82 $workflow->get('type') => $workflow->getTypePlugin()->buildTransitionConfigurationForm($form_state, $workflow, $transition),
92 public function validateForm(array &$form, FormStateInterface $form_state) {
93 /** @var \Drupal\workflows\WorkflowInterface $workflow */
94 $workflow = $this->getEntity();
95 $values = $form_state->getValues();
96 foreach (array_filter($values['from']) as $from_state_id) {
97 if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
98 $transition = $workflow->getTransitionFromStateToState($from_state_id, $values['to']);
99 if ($transition->id() !== $values['id']) {
100 $form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
101 '%from' => $workflow->getState($from_state_id)->label(),
102 '%to' => $workflow->getState($values['to'])->label(),
110 * Copies top-level form values to entity properties
112 * This form can only change values for a state, which is part of workflow.
114 * @param \Drupal\Core\Entity\EntityInterface $entity
115 * The entity the current form should operate upon.
117 * A nested array of form elements comprising the form.
118 * @param \Drupal\Core\Form\FormStateInterface $form_state
119 * The current state of the form.
121 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
122 if (!$form_state->isValidationComplete()) {
123 // Only do something once form validation is complete.
126 /** @var \Drupal\workflows\WorkflowInterface $entity */
127 $values = $form_state->getValues();
128 $form_state->set('created_transition', FALSE);
129 $entity->setTransitionLabel($values['id'], $values['label']);
130 $entity->setTransitionFromStates($values['id'], array_filter($values['from']));
131 if (isset($values['type_settings'])) {
132 $configuration = $entity->getTypePlugin()->getConfiguration();
133 $configuration['transitions'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
134 $entity->set('type_settings', $configuration);
141 public function save(array $form, FormStateInterface $form_state) {
142 /** @var \Drupal\workflows\WorkflowInterface $workflow */
143 $workflow = $this->entity;
145 drupal_set_message($this->t('Saved %label transition.', [
146 '%label' => $workflow->getTransition($this->transitionId)->label(),
148 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
154 protected function actions(array $form, FormStateInterface $form_state) {
155 $actions['submit'] = [
157 '#value' => $this->t('Save'),
158 '#submit' => ['::submitForm', '::save'],
161 $actions['delete'] = [
163 '#title' => $this->t('Delete'),
164 // Deleting a transition is editing a workflow.
165 '#access' => $this->entity->access('edit'),
167 'class' => ['button', 'button--danger'],
169 '#url' => Url::fromRoute('entity.workflow.delete_transition_form', [
170 'workflow' => $this->entity->id(),
171 'workflow_transition' => $this->transitionId