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\Core\Form\SubformState;
9 use Drupal\Core\Plugin\PluginFormFactoryInterface;
11 use Drupal\workflows\State;
12 use Drupal\workflows\TransitionInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Class WorkflowTransitionEditForm.
20 class WorkflowTransitionEditForm extends EntityForm {
23 * The ID of the transition that is being edited.
27 protected $transitionId;
30 * The plugin form factory.
32 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
34 protected $pluginFormFactory;
37 * Creates an instance of WorkflowStateEditForm.
39 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
40 * The plugin form factory.
42 public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
43 $this->pluginFormFactory = $pluginFormFactory;
49 public static function create(ContainerInterface $container) {
51 $container->get('plugin_form.factory')
58 public function getFormId() {
59 return 'workflow_transition_edit_form';
65 public function buildForm(array $form, FormStateInterface $form_state, $workflow_transition = NULL) {
66 $this->transitionId = $workflow_transition;
67 return parent::buildForm($form, $form_state);
73 public function form(array $form, FormStateInterface $form_state) {
74 $form = parent::form($form, $form_state);
76 /* @var \Drupal\workflows\WorkflowInterface $workflow */
77 $workflow = $this->getEntity();
78 $workflow_type = $workflow->getTypePlugin();
79 $transition = $workflow->getTypePlugin()->getTransition($this->transitionId);
82 '#type' => 'textfield',
83 '#title' => $this->t('Transition label'),
85 '#default_value' => $transition->label(),
91 '#value' => $this->transitionId,
94 // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
95 // only valid transitions are selectable.
96 $states = array_map([State::class, 'labelCallback'], $workflow->getTypePlugin()->getStates());
98 '#type' => 'checkboxes',
99 '#title' => $this->t('From'),
101 '#default_value' => array_keys($transition->from()),
102 '#options' => $states,
106 '#title' => $this->t('To'),
108 '#default_value' => $transition->to()->id(),
109 '#options' => $states,
113 // Add additional form fields from the workflow type plugin.
114 if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
115 $form['type_settings'] = [
118 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
119 $subform_state->set('transition', $transition);
120 $form['type_settings'] += $this->pluginFormFactory
121 ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
122 ->buildConfigurationForm($form['type_settings'], $subform_state);
131 public function validateForm(array &$form, FormStateInterface $form_state) {
132 /** @var \Drupal\workflows\WorkflowInterface $workflow */
133 $workflow = $this->getEntity();
134 $workflow_type = $workflow->getTypePlugin();
135 $transition = $workflow_type->getTransition($this->transitionId);
137 $values = $form_state->getValues();
138 foreach (array_filter($values['from']) as $from_state_id) {
139 if ($workflow_type->hasTransitionFromStateToState($from_state_id, $values['to'])) {
140 $existing_transition = $workflow_type->getTransitionFromStateToState($from_state_id, $values['to']);
141 if ($existing_transition->id() !== $values['id']) {
142 $form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
143 '%from' => $workflow->getTypePlugin()->getState($from_state_id)->label(),
144 '%to' => $workflow->getTypePlugin()->getState($values['to'])->label(),
150 if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
151 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
152 $subform_state->set('transition', $transition);
153 $this->pluginFormFactory
154 ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
155 ->validateConfigurationForm($form['type_settings'], $subform_state);
160 * Copies top-level form values to entity properties
162 * This form can only change values for a state, which is part of workflow.
164 * @param \Drupal\Core\Entity\EntityInterface $entity
165 * The entity the current form should operate upon.
167 * A nested array of form elements comprising the form.
168 * @param \Drupal\Core\Form\FormStateInterface $form_state
169 * The current state of the form.
171 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
172 if (!$form_state->isValidationComplete()) {
173 // Only do something once form validation is complete.
176 /** @var \Drupal\workflows\WorkflowInterface $entity */
177 $values = $form_state->getValues();
178 $form_state->set('created_transition', FALSE);
179 $entity->getTypePlugin()->setTransitionLabel($values['id'], $values['label']);
180 $entity->getTypePlugin()->setTransitionFromStates($values['id'], array_filter($values['from']));
186 public function save(array $form, FormStateInterface $form_state) {
187 /** @var \Drupal\workflows\WorkflowInterface $workflow */
188 $workflow = $this->entity;
189 $workflow_type = $workflow->getTypePlugin();
190 $transition = $workflow_type->getTransition($this->transitionId);
192 if ($workflow_type->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
193 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
194 $subform_state->set('transition', $transition);
195 $this->pluginFormFactory
196 ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
197 ->submitConfigurationForm($form['type_settings'], $subform_state);
201 $this->messenger()->addStatus($this->t('Saved %label transition.', [
202 '%label' => $workflow->getTypePlugin()->getTransition($this->transitionId)->label(),
204 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
210 protected function actions(array $form, FormStateInterface $form_state) {
211 $actions['submit'] = [
213 '#value' => $this->t('Save'),
214 '#submit' => ['::submitForm', '::save'],
217 $actions['delete'] = [
219 '#title' => $this->t('Delete'),
220 // Deleting a transition is editing a workflow.
221 '#access' => $this->entity->access('edit'),
223 'class' => ['button', 'button--danger'],
225 '#url' => Url::fromRoute('entity.workflow.delete_transition_form', [
226 'workflow' => $this->entity->id(),
227 'workflow_transition' => $this->transitionId,