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:
- Customer attempts to access form at
- Customer is redirected to customer information webform at
- Customer fills out customer info form and submits it.
- On submission, customer is redirected to
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(); $this->redirect($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?