function drupal_get_destination

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

function drupal_get_destination

https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_...

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

HTTP handling
Functions to properly handle HTTP responses.
28 calls to drupal_get_destination()
File

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

</> CopyGet raw version
php
  1. function drupal_get_destination() {
  2. $destination = &drupal_static(__FUNCTION__);
  3.  
  4. if (isset($destination)) {
  5. return $destination;
  6. }
  7.  
  8. if (isset($_GET ['destination'])) {
  9. $destination = array('destination' => $_GET ['destination']);
  10. }
  11. else {
  12. $path = $_GET ['q'];
  13. $query = drupal_http_build_query(drupal_get_query_parameters());
  14. if ($query != '') {
  15. $path .= '?' . $query;
  16. }
  17. $destination = array('destination' => $path);
  18. }
  19. return $destination;
  20. }

function drupal_encode_path

7 common.inc drupal_encode_path($path)
Encodes a Drupal path for use in a URL.

For aesthetic reasons slashes are not escaped.

Note that url() takes care of calling this function, so a path passed to that function should not be encoded in advance.

</> CopyGet raw version
php
  1. function drupal_encode_path($path) {
  2. return str_replace('%2F', '/', rawurlencode($path));
  3. }

function url

https://api.drupal.org/api/drupal/includes%21common.inc/function/url/7

Generates an internal or external URL.

When creating links in modules, consider whether l() could be a better alternative than url().

Parameters

$path: (optional) The internal path or external URL being linked to, such as "node/34" or "http://example.com/foo". The default value is equivalent to passing in '<front>'. A few notes:

If you provide a full URL, it will be considered an external URL.
If you provide only the path (e.g. "node/34"), it will be considered an internal link. In this case, it should be a system URL, and it will be replaced with the alias, if one exists. Additional query arguments for internal paths must be supplied in $options['query'], not included in $path.
If you provide an internal path and $options['alias'] is set to TRUE, the path is assumed already to be the correct path alias, and the alias is not looked up.
The special string '<front>' generates a link to the site's base URL.
If your external URL contains a query (e.g. http://example.com/foo?a=b), then you can either URL encode the query keys and values yourself and include them in $path, or use $options['query'] to let this function URL encode them.
$options: (optional) An associative array of additional options, with the following elements:

'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
'alias': Defaults to FALSE. Whether the given path is a URL alias already.
'external': Whether the given path is an external URL.
'language': An optional language object. If the path being linked to is internal to the site, $options['language'] is used to look up the alias for the URL. If $options['language'] is omitted, the global $language_url will be used.
'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP, but HTTPS can only be enforced when the variable 'https' is set to TRUE.
'base_url': Only used internally, to modify the base URL when a language dependent URL requires so.
'prefix': Only used internally, to modify the path when a language dependent URL requires so.
'script': The script filename in Drupal's root directory to use when clean URLs are disabled, such as 'index.php'. Defaults to an empty string, as most modern web servers automatically find 'index.php'. If clean URLs are disabled, the value of $path is appended as query parameter 'q' to $options['script'] in the returned URL. When deploying Drupal on a web server that cannot be configured to automatically find index.php, then hook_url_outbound_alter() can be implemented to force this value to 'index.php'.
'entity_type': The entity type of the object that called url(). Only set if url() is invoked by entity_uri().
'entity': The entity object (such as a node) for which the URL is being generated. Only set if url() is invoked by entity_uri().
Return value

A string containing a URL to the given path.

</> CopyGet raw version
php
  1. function url($path = NULL, array $options = array()) {
  2. // Merge in defaults.
  3. $options += array(
  4. 'fragment' => '',
  5. 'query' => array(),
  6. 'absolute' => FALSE,
  7. 'alias' => FALSE,
  8. 'prefix' => ''
  9. );
  10.  
  11. // A duplicate of the code from url_is_external() to avoid needing another
  12. // function call, since performance inside url() is critical.
  13. if (!isset($options ['external'])) {
  14. // Return an external link if $path contains an allowed absolute URL. Avoid
  15. // calling drupal_strip_dangerous_protocols() if there is any slash (/),
  16. // hash (#) or question_mark (?) before the colon (:) occurrence - if any -
  17. // as this would clearly mean it is not a URL. If the path starts with 2
  18. // slashes then it is always considered an external URL without an explicit
  19. // protocol part.
  20. $colonpos = strpos($path, ':');
  21. $options ['external'] = (strpos($path, '//') === 0)
  22. || ($colonpos !== FALSE
  23. && !preg_match('![/?#]!', substr($path, 0, $colonpos))
  24. && drupal_strip_dangerous_protocols($path) == $path);
  25. }
  26.  
  27. // Preserve the original path before altering or aliasing.
  28. $original_path = $path;
  29.  
  30. // Allow other modules to alter the outbound URL and options.
  31. drupal_alter('url_outbound', $path, $options, $original_path);
  32.  
  33. if (isset($options ['fragment']) && $options ['fragment'] !== '') {
  34. $options ['fragment'] = '#' . $options ['fragment'];
  35. }
  36.  
  37. if ($options ['external']) {
  38. // Split off the fragment.
  39. if (strpos($path, '#') !== FALSE) {
  40. list($path, $old_fragment) = explode('#', $path, 2);
  41. // If $options contains no fragment, take it over from the path.
  42. if (isset($old_fragment) && !$options ['fragment']) {
  43. $options ['fragment'] = '#' . $old_fragment;
  44. }
  45. }
  46. // Append the query.
  47. if ($options ['query']) {
  48. $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options ['query']);
  49. }
  50. if (isset($options ['https']) && variable_get('https', FALSE)) {
  51. if ($options ['https'] === TRUE) {
  52. $path = str_replace('http://', 'https://', $path);
  53. }
  54. elseif ($options ['https'] === FALSE) {
  55. $path = str_replace('https://', 'http://', $path);
  56. }
  57. }
  58. // Reassemble.
  59. return $path . $options ['fragment'];
  60. }
  61.  
  62. // Strip leading slashes from internal paths to prevent them becoming external
  63. // URLs without protocol. /example.com should not be turned into
  64. // //example.com.
  65. $path = ltrim($path, '/');
  66.  
  67. global $base_url, $base_secure_url, $base_insecure_url;
  68.  
  69. // The base_url might be rewritten from the language rewrite in domain mode.
  70. if (!isset($options ['base_url'])) {
  71. if (isset($options ['https']) && variable_get('https', FALSE)) {
  72. if ($options ['https'] === TRUE) {
  73. $options ['base_url'] = $base_secure_url;
  74. $options ['absolute'] = TRUE;
  75. }
  76. elseif ($options ['https'] === FALSE) {
  77. $options ['base_url'] = $base_insecure_url;
  78. $options ['absolute'] = TRUE;
  79. }
  80. }
  81. else {
  82. $options ['base_url'] = $base_url;
  83. }
  84. }
  85.  
  86. // The special path '<front>' links to the default front page.
  87. if ($path == '<front>') {
  88. $path = '';
  89. }
  90. elseif (!empty($path) && !$options ['alias']) {
  91. $language = isset($options ['language']) && isset($options ['language']->language) ? $options ['language']->language : '';
  92. $alias = drupal_get_path_alias($original_path, $language);
  93. if ($alias != $original_path) {
  94. $path = $alias;
  95. }
  96. }
  97.  
  98. $base = $options ['absolute'] ? $options ['base_url'] . '/' : base_path();
  99. $prefix = empty($path) ? rtrim($options ['prefix'], '/') : $options ['prefix'];
  100.  
  101. // With Clean URLs.
  102. if (!empty($GLOBALS ['conf']['clean_url'])) {
  103. $path = drupal_encode_path($prefix . $path);
  104. if ($options ['query']) {
  105. return $base . $path . '?' . drupal_http_build_query($options ['query']) . $options ['fragment'];
  106. }
  107. else {
  108. return $base . $path . $options ['fragment'];
  109. }
  110. }
  111. // Without Clean URLs.
  112. else {
  113. $path = $prefix . $path;
  114. $query = array();
  115. if (!empty($path)) {
  116. $query ['q'] = $path;
  117. }
  118. if ($options ['query']) {
  119. // We do not use array_merge() here to prevent overriding $path via query
  120. // parameters.
  121. $query += $options ['query'];
  122. }
  123. $query = $query ? ('?' . drupal_http_build_query($query)) : '';
  124. $script = isset($options ['script']) ? $options ['script'] : '';
  125. return $base . $script . $query . $options ['fragment'];
  126. }
  127. }