Page Redirection on Drupal 8 / 9 | Drupal 8

Page Redirection on Drupal 8 / 9

Submitted by editor on Wed, 12/23/2015 - 10:51

How to make a redirection on Drupal 8 (like drupal_goto() on Drupal 7)?

Example 1. Redirect to the front page:

    return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('<front>'));

Example 2. Redirect to a route path (user page):

    return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url(''));

Example 3. To a internal path

   return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/17/edit');
   return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal\Core\Url::fromUserInput('/node/17/edit')->toString());

Example 4. Redirect to Access Denied (403) or Not Found (404) page.

throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();

throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();


Redirect to external domain

throw new \Drupal\Core\Routing\TrustedRedirectResponse('');


See also : Create a link with Drupal 8 like l() and url() on D7


You can process your custom change using HOOK_form_alter

Example :
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   if($form_id == 'YOUR_NODE_FORM_ID') {
     kint($form['field_name']['widget']); // show the value and field values
     $form['field_name']['widget'][0]['value']['#default_value'] = 'My new value'; // for textfield.


Shobhana (not verified)

Mon, 08/08/2016 - 13:45


I want to redirect the page when node submission is greater than 1 , i have used hook_node_presave in drupal 8

But could not understand how to redirect without saving node.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.