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

Prepares a 'destination' URL query parameter for use 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.

Return value

An associative array containing the key:

  • destination: The path provided via the destination query string or, if not available, the current path.

See also

current_path()

drupal_goto()

Related topics

28 calls to drupal_get_destination()
comment_admin_overview in modules/comment/comment.admin.inc
Form builder for the comment overview administration form.
common_test_destination in modules/simpletest/tests/common_test.module
Print destination query parameter.
contextual_pre_render_links in modules/contextual/contextual.module
Build a renderable array for contextual links.
field_ui_field_overview_form_submit in modules/field_ui/field_ui.admin.inc
Form submission handler for field_ui_field_overview_form().
forum_menu_local_tasks_alter in modules/forum/forum.module
Implements hook_menu_local_tasks_alter().

... See full list

File

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

Code

function drupal_get_destination() {
  $destination = &drupal_static(__FUNCTION__);

  if (isset($destination)) {
    return $destination;
  }

  if (isset($_GET['destination'])) {
    $destination = array('destination' => $_GET['destination']);
  }
  else {
    $path = $_GET['q'];
    $query = drupal_http_build_query(drupal_get_query_parameters());
    if ($query != '') {
      $path .= '?' . $query;
    }
    $destination = array('destination' => $path);
  }
  return $destination;
}

Comments

I'm trying to redirect users back to a Panels page after creating a new content item, but in D7 I cannot get the proper destination. Here is the code which I insert into a view header:

<?php
global $base_url;
$destination = drupal_get_destination();
$url = $base_url . '/node/add/discount-product?';
print
'<a class="add-new" id="add-new-discount-product" href="' . $url . $destination . '">Add new Discount Product</a>';
?>

D6 returns the destination properly, but D7 returns "Array"

drupal_get_destination() returns an array in Drupal 7, but returned a string in Drupal 6, so what you want is

<?php
print '<a class="add-new" id="add-new-discount-product" href="' . $url . '?destination=' . $destination['destination'] . '">Add new Discount Product</a>';
?>

Really though, what you want is:

<?php
$options
= array(
 
'query' => drupal_get_destination(),
 
'attributes' => array(
   
'class' => array('add-new'),
   
'id' => 'add-new-discount-product',
  ),
);
print
l(t('Add new Discount Product'), $url, $options);
?>

$destination = drupal_get_destination();
$destination = $destination['destination'];

Thank you rogical. Works great in a spot where I could not use print (l).

Would be nice to have a matching origin function so we can use the goto at the destination. I use:

function drupal_get_origin() {
  /* @var $_SERVER HTTP_REFERER works on our servers. */
  $path = $_SERVER['HTTP_REFERER'];
  if (isset($_GET['destination'])) {
    $path = $_GET['destination'];
  }
  return $path;
}

@peterx HTTP_REFERER is not provided by the server. It's up to the client whether it sends this information to you so you should never rely on it's presence or assume that it will be correct.

I haven't seen it happen often but it can.

This is from the PHP $_SERVER doc page:

HTTP_REFERER

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

thx Angry Dan for the tip.

Here is an exemple with drupal_goto()

<?php

$options

= array('query' => drupal_get_destination());

drupal_goto('user', $options);
?>

et voilà ;)