I have a use case where I'm creating a custom form, but for business reasons, I need to obtain customer information in a separate webform prior to granting access to my custom form. So my workflow needs to be as follows:

  1. Customer attempts to access form at /my-custom-form url
  2. Customer is redirected to customer information webform at /customer-information-form
  3. Customer fills out customer info form and submits it.
  4. On submission, customer is redirected to /my-custom-form url.

My thought was to use a controller for the /my-custom-form url, and just do a redirect to the webform, and then when the webform has been submitted, redirect back to the form, having to note that the webform is submitted. However, what I'm having problems finding is how to redirect using an internal URL using the Drupal\Core\Url class. In order to use the redirect() method that is included in the ControllerBase class, it has to be a defined route name (e.g. my_module.my_custom_route. If I try something like this:

$url_object = \Drupal::service('path.validator') ->getUrlIfValid('/customer-information-form');
$route_name = $url_object->getRouteName();

$route_name = entity.webform.canonical, which throws an error, since that's a generic route name.

So my question is, how do I redirect my user to my webform using an internal path, especially when it is just a generic webform?

UPDATE: So the ControllerBase class has a redirect() method that from what I've seen, should allow me to redirect. However, when I use this code in my controller function:

$url_object = \Drupal::service('path.validator')->getUrlIfValid('/my-custom-form');
$route_name = $url_object->getRouteName();
$route_params = $url_object->getRouteParameters();
$this->redirect($route_name, $route_params);

but when I run it, I get an error that my controller must return a response. If I add some simple markup like so

return [
  '#type' => 'markup',
  '#markup' => $this->t('Hello, World!'),

Then I get that markup instead of the redirect happening. What is the purpose of that redirect() method if it doesn't redirect?

  • When you have a _form route for your custom form Drupal wraps a controller around the form automatically and you can redirect from inside of the form, see drupal.stackexchange.com/a/277596/47547
    – 4uk4
    Commented Sep 27, 2020 at 17:57
  • Does this answer your question? How to redirect to a page after submitting a form
    – No Sssweat
    Commented Sep 27, 2020 at 18:01
  • @NoSssweat no, because I want to redirect to my webform when they first access the page with my custom form, not after submitting my form.
    – wonder95
    Commented Sep 27, 2020 at 18:06
  • Well you going to endup with a refirect loop on #4 unless you use $_SESSION or cookie to track that they submitted webform.
    – No Sssweat
    Commented Sep 27, 2020 at 18:09
  • 1
    Right, well, to avoid that, I will definitely need some code in my controller that tells me that the webform has been submitted, probably by url param or something similar; I just didn't mention it in the question.
    – wonder95
    Commented Sep 27, 2020 at 18:11

2 Answers 2


Personally, I would create a multi step/page webform or multi step/page custom form instead of having two separate forms, but to answer your question:

In your webform settings:

enter image description here

In your custom form code

  public function buildForm(array $form, FormStateInterface $form_state) {
    // redirect
    if (!\Drupal::request()->query->get('hello') == 'world') { // if url doesn't have ?hello=world
      $path = '/customer-information-form';
      $response = new \Symfony\Component\HttpFoundation\RedirectResponse($path);
  • Well, in the bigger picture, this webform needs to be separate from the custom form, because there are other forms throughout the site that need to utilize the same functionality. But thanks for the redirect code, that works.
    – wonder95
    Commented Sep 27, 2020 at 18:58
  • 1
    Well, $this-redirect() does the same, creating a redirect response. But then, don't send it, return it.
    – 4uk4
    Commented Sep 27, 2020 at 19:06
  • With what app did you do the screenshot and the drawing? Monosnap suddenly started a ridiculous pricing model. I'm looking for a new one on Mac.
    – leymannx
    Commented Sep 27, 2020 at 20:28
  • 1
    @leymannx it's an app that comes with Windows 10
    – No Sssweat
    Commented Sep 27, 2020 at 21:13

What is the purpose of that redirect() method if it doesn't redirect?

You find this method in both, controller and form:



It creates a redirect response and you return it if a certain condition is met:

if (...) {
   return $this->redirect($route_name);

Example: https://drupal.stackexchange.com/a/277596/47547

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.