drupal_get_destination

5 common.inc drupal_get_destination()
6 common.inc drupal_get_destination()
7 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()

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

A note on the output

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

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

drupal_get_destination_alias()

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>

Code causes parsing error

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.

<front> to redirect to front page

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

What about #hash tags?

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

Login or register to post comments