Using setRedirect() for post-submission redirectsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In our continuing series on the Drupal Form API, we previously discussed accessing form values within the submitForm() function. In this lesson, we will explore how to guide users seamlessly to different pages after they submit a form using setRedirect().

Understanding setRedirect()

The setRedirect() function in the Drupal Form API enables developers to redirect users to a specific page after form submission. This functionality is critical for enhancing the user experience, by providing feedback, facilitating workflows, or directing users to subsequent actions post-submission.

Benefits of Using setRedirect()

  • Improved User Navigation: Helps users move effortlessly through different stages or pages after form completion.
  • Enhanced Workflow: Directs users to appropriate follow-up actions or confirmation pages, improving process efficiency.
  • User Feedback: Provides instant user feedback by leading them to pages that reflect their input or subsequent interaction areas.

Implementing setRedirect()

Let’s implement a scenario where, upon successful form submission, users are directed to a custom confirmation page:

function mymodule_form_submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    // Accessing form values
    $name = $form_state->getValue('name');
    
    // Display a confirmation message
    \Drupal::messenger()->addMessage(t('Thank you, @name, your submission was successful!', ['@name' => $name]));
    
    // Redirect to a confirmation page
    $form_state->setRedirect('mymodule.confirmation_page');
}
    

Here's what happens in this code:

  • Retrieve the submitted name and thank the user with a personalized message.
  • The setRedirect() method specifies a route to redirect the user to a confirmation page.

Creating a Custom Route

To utilize setRedirect() effectively, your target page must have a defined route. Let's define a simple route in the mymodule.routing.yml file:

mymodule.confirmation_page:
  path: '/confirmation'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController::confirmation'
    _title: 'Submission Confirmation'
  requirements:
    _permission: 'access content'
    

In this routing.yml setup, we create a route pointing to a custom controller that will render the confirmation page.

Redirecting with Parameters

Sometimes, you may wish to pass parameters along with your redirection. Here is an example:

$form_state->setRedirect('mymodule.confirmation_page', ['param1' => $value1, 'param2' => $value2]);
    

In this instance, param1 and param2 are placeholders in your route path that will be replaced with their corresponding values upon redirection.

Best Practices

  • Consistency: Ensure shared principles across redirects to maintain predictable user navigation flows.
  • Clarity: Use clear and informative endpoint descriptions or titles for destination pages.
  • Security: Validate redirect targets and user access permissions to prevent unauthorized access or redirection loops.

Conclusion

By implementing setRedirect(), you create a smoother and more intuitive user experience, effectively guiding users through your web application’s workflow. Mastering this function will empower your forms with advanced navigational capabilities.

Coming Up: Saving Form Data with config.factory

In the next lesson, we will explore saving form data using Drupal’s configuration management service, config.factory. This approach provides a robust way to persist and manage configuration data within your Drupal site. Join us as we continue our journey through Drupal's Form API!