public static function FormsStepsAlter::setNextRoute in Forms Steps 8
Redirect to the next step if one exists.
Parameters
array $form: Form to alter.
\Drupal\Core\Form\FormState $form_state: Form State to update.
File
- src/
Form/ FormsStepsAlter.php, line 93
Class
- FormsStepsAlter
- Class FormsStepsAlter.
Namespace
Drupal\forms_steps\FormCode
public static function setNextRoute(array &$form, FormState $form_state) {
/** @var \Drupal\forms_steps\Service\FormsStepsManager $formsStepsManager */
$route = \Drupal::routeMatch();
$route_name = $route
->getRouteName();
$formsStepsManager = \Drupal::service('forms_steps.manager');
/** @var \Drupal\forms_steps\Entity\Workflow $workflowManager */
$workflowManager = \Drupal::service('forms_steps.workflow.manager');
$nextRoute = $formsStepsManager
->getNextStepRoute($route_name);
if ($nextRoute) {
$workflow = $workflowManager
->getWorkflowByEntity($form_state
->getFormObject()
->getEntity());
$form_state
->setRedirect($nextRoute, [
'instance_id' => $workflow->instance_id->value,
]);
}
// Set redirection on final step according to redirection policy.
$forms_steps = \Drupal::service('forms_steps.manager')
->getFormsStepsByRoute($route_name);
$redirection_policy = $forms_steps
->getRedirectionPolicy();
if ($redirection_policy != '') {
$step = $formsStepsManager
->getStepByRoute($route_name);
if ($step
->isLast()) {
$redirection_target = $forms_steps
->getRedirectionTarget();
switch ($redirection_policy) {
case 'internal':
$target_url = \Drupal::service('path.validator')
->getUrlIfValid($redirection_target);
$target_route_name = $target_url
->getRouteName();
$target_route_parameters = $target_url
->getrouteParameters();
$form_state
->setRedirect($target_route_name, $target_route_parameters);
break;
case 'route':
$parameters = $route
->getParameters()
->all();
$form_state
->setRedirect($redirection_target, $parameters);
break;
case 'external':
$form_state
->setResponse(new TrustedRedirectResponse($redirection_target, 302));
break;
case 'entity':
$parameters = $route
->getParameters()
->all();
// Add current entity to parameters.
$current_entity = $form_state
->getformObject()
->getEntity();
if (isset($current_entity)) {
$parameters[$current_entity
->getEntityTypeId()] = $current_entity
->id();
}
$form_state
->setRedirect('entity.node.canonical', $parameters);
break;
}
}
}
// Dispatch a STEP_CHANGE_EVENT event.
$step = $formsStepsManager
->getStepByRoute($route_name);
$jumpStep = $formsStepsManager
->getStepByRoute($nextRoute);
$jumpStepEvent = new StepChangeEvent($forms_steps, $step, $jumpStep, $form_state);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher
->dispatch(StepChangeEvent::STEP_CHANGE_EVENT, $jumpStepEvent);
}