function drupal_get_destination

You are here

7 common.inc drupal_get_destination()
4.6 common.inc drupal_get_destination()
4.7 common.inc drupal_get_destination()
5 common.inc drupal_get_destination()
6 common.inc drupal_get_destination()
8 common.inc drupal_get_destination()

Prepare a destination query string for use in combination with drupal_goto().

Used to direct the user back to the referring page after completing a form. By default the current URL is returned. If a destination exists in the previous request, that destination is returned. As such, a destination can persist across multiple pages.

See also

drupal_goto()

Related topics

16 calls to drupal_get_destination()
comment_admin_overview in modules/comment/comment.admin.inc
Form builder; Builds the comment overview form for the admin.
hook_translated_menu_link_alter in developer/hooks/core.php
Alter a menu link after it's translated, but before it's rendered.
node_admin_nodes in modules/node/node.admin.inc
Form builder: Builds the node administration overview.
node_form_delete_submit in modules/node/node.pages.inc
Button sumit function: handle the 'Delete' button on the node form.
openid_authentication in modules/openid/openid.module
Authenticate a user or attempt registration.

... See full list

File

includes/common.inc, line 261
Common functions that many Drupal modules will need to reference.

Code

function drupal_get_destination() {
  if (isset($_REQUEST['destination'])) {
    return 'destination=' . urlencode($_REQUEST['destination']);
  }
  else {
    // Use $_GET here to retrieve the original path in source form.
    $path = isset($_GET['q']) ? $_GET['q'] : '';
    $query = drupal_query_string_encode($_GET, array('q'));
    if ($query != '') {
      $path .= '?' . $query;
    }
    return 'destination=' . urlencode($path);
  }
}

Comments

This returns destination=node/ID. It has the destination text at the beginning and ignores path aliases. If you want to use path aliases, use

<?php
print  "destination=". drupal_get_path_alias($_GET['q']);
?>

it is better to use drupal_get_destination, but make sure that you post-proccess the node/345 to an alias - here is how:
See here http://drupal.org/project/login_destination for a smarter function. (Look into the php snippet there.)

Here's a helpful function that takes things a bit further. This should give you back an aliased destination, if it exists:

<?php
function drupal_get_destination_alias() {
  if (isset(
$_REQUEST['destination'])) {
    return
'destination='. urlencode(drupal_get_path_alias($_REQUEST['destination']));
  }
  else {
   
// Use $_GET here to retrieve the original path in source form.
   
$path = isset($_GET['q']) ? $_GET['q'] : '';
   
$query = drupal_query_string_encode($_GET, array('q'));
    if (
$query != '') {
     
$path .= '?'. $query;
    }
    return
'destination='. urlencode(drupal_get_path_alias($path));
  }
}
?>

Usage Example:

<a href="http://www.yoursite.com/node/add/page?<?php print drupal_get_destination_alias(); ?>">Go create a page and then come back here (or to the page that brought you here).</a>

I have a similar use for this code but it produces a parsing error:

Parse error: syntax error, unexpected T_IF, expecting '{' in /usr/local/www/apache22/data/includes/common.inc(1695) : eval()'d code on line 3

I pasted this code into a Views Custom Field, to be displayed as a link button in a block:

<?php
function drupal_get_destination_alias() {
  if (isset(
$_REQUEST['destination'])) {
    return
'destination='. urlencode(drupal_get_path_alias($_REQUEST['destination']));
  }
  else {
   
// Use $_GET here to retrieve the original path in source form.
   
$path = isset($_GET['q']) ? $_GET['q'] : '';
   
$query = drupal_query_string_encode($_GET, array('q'));
    if (
$query != '') {
     
$path .= '?'. $query;
    }
    return
'destination='. urlencode(drupal_get_path_alias($path));
  }
}

$path1 = drupal_get_destination_alias();
print (
"<a href="" . $path1 . "/buildings" title="View">View</a>");
?>

The idea is to take the path from the page the Views block display is on, like "content/chicago/southside" and when the button is clicked, take the user to a page with a view with a path like "content/chicago/southside/buildings". "Chicago" and "southside" are passed as arguments to the view.

Shouldn't be that hard to do this, but I can't get anything I try to work.

UPDATE:

It turns out that there is a bug in Views Custom Field that creates a parsing error if you try to define a function.

I just put this code in a regular block with the PHP input filter enabled and it now works:

<?php
function aarg($index = NULL, $path = NULL)
{
  if (!isset(
$path)) {
   
$path = check_plain(drupal_get_path_alias($_GET['q']));
  }
  if (!isset(
$arguments[$path])) {
   
$arguments[$path] = explode('/', $path);
  }
  if (!isset(
$index)) {
    return
$arguments[$path];
  }
  if (isset(
$arguments[$path][$index])) {
    return
$arguments[$path][$index];
  }
}

$city = aarg(1);
$area = aarg(2);
print
l('View buildings', "content/$city/$area/buildings");

?>

I got the aarg function from a comment here: http://drupal.org/node/345877 and added the checkplain function to sanitize for XSS. It's a modified version of arg() to return the path alias - arg() by itself returns "node/[nid]" and I needed the alias to feed to Views as arguments.

Note that ?destination=<front> is a perfectly valid and working parameter (destination goes through url()).

Is there a way to extend the destination value by including any "#values" in the URL? Whenever I visit a page that uses a #named-anchor in the URL, and hit "Edit" for the node, the destination value strips out any "#" data...

If you're trying to redirect users to a specific anchor within a page you could look to see if the 'destination' exists in the request and then do a str_replace() on the value, for example:

if (isset($_GET['destination'])) {
      $dest = str_replace('#', '%23', $_GET['destination']);
}

but it appears that if you use something like ?destination=comment/11977#comment-11977
then the # and following is already disregarded before it gets into $_GET.
Therefore you must construct the destination querystring with %23 instead of #.