Question

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();
$this->redirect($route_name);

$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?

Was it helpful?

Solution

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);
      $response->send();
    }

OTHER TIPS

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

You find this method in both, controller and form:

ControllerBase::redirect()

FormBase::redirect()

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

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top