You are here

public static function FormsStepsAlter::setPreviousRoute in Forms Steps 8

Redirect the form to the previous step.

Parameters

array $form: Form to alter.

\Drupal\Core\Form\FormState $form_state: Forms State to Update.

File

src/Form/FormsStepsAlter.php, line 170

Class

FormsStepsAlter
Class FormsStepsAlter.

Namespace

Drupal\forms_steps\Form

Code

public static function setPreviousRoute(array &$form, FormState $form_state) {

  /** @var \Drupal\forms_steps\Service\FormsStepsManager $formsStepsManager */
  $route = \Drupal::routeMatch();
  $route_name = $route
    ->getRouteName();

  /** @var \Drupal\forms_steps\Service\FormsStepsManager $formsStepsManager */
  $formsStepsManager = \Drupal::service('forms_steps.manager');
  $previousRoute = $formsStepsManager
    ->getPreviousStepRoute($route_name);
  if ($previousRoute) {

    /** @var \Drupal\forms_steps\Entity\Workflow $workflowManager */
    $workflowManager = \Drupal::service('forms_steps.workflow.manager');

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    if ($entity
      ->isNew()) {

      // For the moment, new entity form doesn't contains the instance_id,
      // hence we need to get it from URL.
      $instanceId = \Drupal::routeMatch()
        ->getParameter('instance_id');
    }
    else {
      $workflow = $workflowManager
        ->getWorkflowByEntity($form_state
        ->getFormObject()
        ->getEntity());
      $instanceId = $workflow->instance_id->value;
    }
    $form_state
      ->setRedirect($previousRoute, [
      'instance_id' => $instanceId,
    ]);
  }
}