How to redirect to another page in Drupal 7?

by dalton_moen , in category: PHP Frameworks , 2 years ago

How to redirect to another page in Drupal 7?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by kadin , 2 years ago

@dalton_moen 

To redirect to another page in Drupal 7, you can use the drupal_goto() function. This function is provided by the Drupal system and is used to redirect the user to a different page on the site.


Here's an example of how to use drupal_goto() to redirect the user to the homepage:

1
drupal_goto('<front>');


You can also specify an external URL as the destination:

1
drupal_goto('https://www.example.com');


You can use drupal_goto() in a variety of contexts, such as in a form submission handler, in a custom function, or in a template file.


Note: drupal_goto() is deprecated in Drupal 8 and has been removed in Drupal 9. In Drupal 8 and later, you should use the RedirectResponse class from the Symfony HTTP Foundation component to redirect the user to another page.

Member

by lily , a year ago

@dalton_moen 

In Drupal 7, you can redirect to another page programmatically using the drupal_goto() function. Here's an example:

  1. In your custom module or theme, create a new function or modify an existing one where you want the redirect to happen.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function mymodule_redirect_to_another_page() {
  // Replace 'path/to/destination' with the path to the page you want to redirect to.
  $path = 'path/to/destination';

  // Specify additional options, if needed.
  $options = array(
    'query' => array('param1' => 'value1', 'param2' => 'value2'),
    'absolute' => TRUE,
  );

  // Perform the redirect.
  drupal_goto($path, $options);
}


  1. Save your changes and clear Drupal's caches for the new function to take effect.


Now, when you call mymodule_redirect_to_another_page(), Drupal will redirect the user to the specified page, with optional query parameters and an absolute URL if necessary.