Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Page Redirection on Drupal 8 / 9

By editor , 23 December, 2015
Question

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('user.page'));

Example 3. To a internal path

   return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/17/edit');
   OR
   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.

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

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

 

Redirect to external domain

throw new \Drupal\Core\Routing\TrustedRedirectResponse('https://example.com');

 

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

Tags

  • Drupal 8
  • Code
  • Drupal 9
  • Add new comment
Form redirection on submit
Create a link with Drupal 8 like l() and url() on D7
Redirect a user after login. Redirection on Drupal 8.

Comments8

Plain text

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

anil280988 (not verified)

10 years 8 months ago

Redirect to internal page

How to redirect to internal pages, like node/17/edit.

  • Reply

editor

10 years 6 months ago

In reply to Redirect to internal page by anil280988 (not verified)

You can simply do like this:…

You can simply do like this:

return new \Symfony\Component\HttpFoundation\RedirectResponse('/node/17/edit');
NOTE : Don't foget '/node/17/edit' NOT 'node/17/edit'

 

  • Reply

Kalaiyarasi (not verified)

9 years 4 months ago

In reply to Redirect to internal page by anil280988 (not verified)

How do I get my form values into content type

After submitting  form am redirecting into content type. How do I get my form values into my content type filed(using POST,Session variable). How do I get my form values into content type(node/add/add-form)add-form is my content type.

  • Reply

editor

9 years 4 months ago

In reply to How do I get my form values into content type by Kalaiyarasi (not verified)

Use form alter

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.
  }
}

 

  • Reply

Shobhana (not verified)

10 years 3 months ago

Redirect form hook_node_presave

Hi,

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.

  • Reply

thachakrit (not verified)

9 years 7 months ago

I'm very new. Just one…

I'm very new. Just one question, Where and what files I have to put these codes ?

  • Reply

editor

9 years 7 months ago

In reply to I'm very new. Just one… by thachakrit (not verified)

Create a new modul

Hi,
You must create a new module like this. Then use it in the YOURMODUL.module or in a controler

  • Reply

Tiffany Wen (not verified)

5 years 5 months ago

How to redirect user to page before access denied after login

User wanted to view a page but got access denied. User logged in and back to access denied page again. How to redirect user to the first page he/she want to view? Thanks.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer