redirectForm example

// Execution continues only for programmatic forms.       // For 'regular' forms, we get redirected to the batch processing       // page. Form redirection will be handled in _batch_finished(),       // after the batch is processed.     }

    // Set a flag to indicate the form has been processed and executed.     $form_state->setExecuted();

    // If no response has been set, process the form redirect.     if (!$form_state->getResponse() && $redirect = $this->redirectForm($form_state)) {
      $form_state->setResponse($redirect);
    }

    // If there is a response was set, return it instead of continuing.     if (($response = $form_state->getResponse()) && $response instanceof Response) {
      return $response;
    }
  }

  /** * {@inheritdoc} */
/** * {@inheritdoc} */
  public function validateForm($form_id, &$form, FormStateInterface &$form_state) {
    $this->formValidator->validateForm($form_id$form$form_state);
  }

  /** * {@inheritdoc} */
  public function redirectForm(FormStateInterface $form_state) {
    return $this->formSubmitter->redirectForm($form_state);
  }

  /** * {@inheritdoc} */
  public function executeValidateHandlers(&$form, FormStateInterface &$form_state) {
    $this->formValidator->executeValidateHandlers($form$form_state);
  }

  /** * {@inheritdoc} */
$form_state = $this->createMock('Drupal\Core\Form\FormStateInterface');
    $form_state->expects($this->once())
      ->method('getRedirect')
      ->willReturn(NULL);

    $this->urlGenerator->expects($this->once())
      ->method('generateFromRoute')
      ->with('<current>', []['query' => [], 'absolute' => TRUE])
      ->willReturn('http://localhost/test-path');

    $redirect = $form_submitter->redirectForm($form_state);
    // If we have no redirect, we redirect to the current URL.     $this->assertSame('http://localhost/test-path', $redirect->getTargetUrl());
    $this->assertSame(303, $redirect->getStatusCode());
  }

  /** * Tests redirectForm() when a redirect is a Url object. * * @covers ::redirectForm * * @dataProvider providerTestRedirectWithUrl */
Home | Imprint | This part of the site doesn't use cookies.