3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\workflows\State;
11 * Class WorkflowTransitionAddForm.
13 class WorkflowTransitionAddForm extends EntityForm {
18 public function getFormId() {
19 return 'workflow_transition_add_form';
25 public function form(array $form, FormStateInterface $form_state) {
26 $form = parent::form($form, $form_state);
28 /* @var \Drupal\workflows\WorkflowInterface $workflow */
29 $workflow = $this->getEntity();
31 '#type' => 'textfield',
32 '#title' => $this->t('Label'),
34 '#default_value' => '',
35 '#description' => $this->t('Label for the transition.'),
40 '#type' => 'machine_name',
42 'exists' => [$this, 'exists'],
46 // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
47 // only valid transitions are selectable.
48 $states = array_map([State::class, 'labelCallback'], $workflow->getStates());
50 '#type' => 'checkboxes',
51 '#title' => $this->t('From'),
53 '#default_value' => [],
54 '#options' => $states,
58 '#title' => $this->t('To'),
60 '#default_value' => [],
61 '#options' => $states,
64 // Add additional form fields from the workflow type plugin.
65 $form['type_settings'] = [
66 $workflow->get('type') => $workflow->getTypePlugin()->buildTransitionConfigurationForm($form_state, $workflow),
74 * Determines if the workflow transition already exists.
76 * @param string $transition_id
77 * The workflow transition ID.
80 * TRUE if the workflow transition exists, FALSE otherwise.
82 public function exists($transition_id) {
83 /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
84 $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
85 return $original_workflow->hasTransition($transition_id);
89 * Copies top-level form values to entity properties
91 * This form can only change values for a state, which is part of workflow.
93 * @param \Drupal\Core\Entity\EntityInterface $entity
94 * The entity the current form should operate upon.
96 * A nested array of form elements comprising the form.
97 * @param \Drupal\Core\Form\FormStateInterface $form_state
98 * The current state of the form.
100 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
101 if (!$form_state->isValidationComplete()) {
102 // Only do something once form validation is complete.
105 /** @var \Drupal\workflows\WorkflowInterface $entity */
106 $values = $form_state->getValues();
107 $entity->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
108 if (isset($values['type_settings'])) {
109 $configuration = $entity->getTypePlugin()->getConfiguration();
110 $configuration['transitions'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
111 $entity->set('type_settings', $configuration);
119 public function validateForm(array &$form, FormStateInterface $form_state) {
120 /** @var \Drupal\workflows\WorkflowInterface $workflow */
121 $workflow = $this->getEntity();
122 $values = $form_state->getValues();
123 foreach (array_filter($values['from']) as $from_state_id) {
124 if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
125 $form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
126 '%from' => $workflow->getState($from_state_id)->label(),
127 '%to' => $workflow->getState($values['to'])->label(),
136 public function save(array $form, FormStateInterface $form_state) {
137 /** @var \Drupal\workflows\WorkflowInterface $workflow */
138 $workflow = $this->entity;
140 drupal_set_message($this->t('Created %label transition.', [
141 '%label' => $form_state->getValue('label'),
143 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
149 protected function actions(array $form, FormStateInterface $form_state) {
150 $actions['submit'] = [
152 '#value' => $this->t('Save'),
153 '#submit' => ['::submitForm', '::save'],