path.inc

  1. drupal
    1. 4.7 includes/path.inc
    2. 5 includes/path.inc
    3. 6 includes/path.inc
    4. 7 includes/path.inc
    5. 8 core/includes/path.inc

Functions to handle paths in Drupal, including path aliasing.

These functions are not loaded for cached pages, but modules that need to use them in hook_init() or hook exit() can make them available, by executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);".

Functions & methods

NameDescription
argReturn a component of the current Drupal path.
drupal_get_normal_pathGiven a path alias, return the internal path it represents.
drupal_get_path_aliasGiven an internal Drupal path, return the alias set by the administrator.
drupal_get_titleGet the title of the current page, for display on the page and in the title bar.
drupal_init_pathInitialize the $_GET['q'] variable to the proper normal path.
drupal_is_front_pageCheck if the current page is the front page.
drupal_lookup_pathGiven an alias, return its Drupal system URL if one exists. Given a Drupal system URL return one of its aliases if such a one exists. Otherwise, return FALSE.
drupal_set_titleSet the title of the current page, for display on the page and in the title bar.

File

includes/path.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions to handle paths in Drupal, including path aliasing.
  5. *
  6. * These functions are not loaded for cached pages, but modules that need
  7. * to use them in hook_init() or hook exit() can make them available, by
  8. * executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);".
  9. */
  10. /**
  11. * Initialize the $_GET['q'] variable to the proper normal path.
  12. */
  13. function drupal_init_path() {
  14. if (!empty($_GET['q'])) {
  15. $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
  16. }
  17. else {
  18. $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  19. }
  20. }
  21. /**
  22. * Given an alias, return its Drupal system URL if one exists. Given a Drupal
  23. * system URL return one of its aliases if such a one exists. Otherwise,
  24. * return FALSE.
  25. *
  26. * @param $action
  27. * One of the following values:
  28. * - wipe: delete the alias cache.
  29. * - alias: return an alias for a given Drupal system path (if one exists).
  30. * - source: return the Drupal system URL for a path alias (if one exists).
  31. * @param $path
  32. * The path to investigate for corresponding aliases or system URLs.
  33. *
  34. * @return
  35. * Either a Drupal system path, an aliased path, or FALSE if no path was
  36. * found.
  37. */
  38. function drupal_lookup_path($action, $path = '') {
  39. // $map keys are Drupal paths and the values are the corresponding aliases
  40. static $map = array(), $no_src = array();
  41. static $count;
  42. // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
  43. if (!isset($count)) {
  44. $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
  45. }
  46. if ($action == 'wipe') {
  47. $map = array();
  48. $no_src = array();
  49. }
  50. elseif ($count > 0 && $path != '') {
  51. if ($action == 'alias') {
  52. if (isset($map[$path])) {
  53. return $map[$path];
  54. }
  55. $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
  56. $map[$path] = $alias;
  57. return $alias;
  58. }
  59. // Check $no_src for this $path in case we've already determined that there
  60. // isn't a path that has this alias
  61. elseif ($action == 'source' && !isset($no_src[$path])) {
  62. // Look for the value $path within the cached $map
  63. if (!$src = array_search($path, $map)) {
  64. if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
  65. $map[$src] = $path;
  66. }
  67. else {
  68. // We can't record anything into $map because we do not have a valid
  69. // index and there is no need because we have not learned anything
  70. // about any Drupal path. Thus cache to $no_src.
  71. $no_src[$path] = TRUE;
  72. }
  73. }
  74. return $src;
  75. }
  76. }
  77. return FALSE;
  78. }
  79. /**
  80. * Given an internal Drupal path, return the alias set by the administrator.
  81. *
  82. * @param $path
  83. * An internal Drupal path.
  84. *
  85. * @return
  86. * An aliased path if one was found, or the original path if no alias was
  87. * found.
  88. */
  89. function drupal_get_path_alias($path) {
  90. $result = $path;
  91. if ($alias = drupal_lookup_path('alias', $path)) {
  92. $result = $alias;
  93. }
  94. if (function_exists('custom_url_rewrite')) {
  95. $result = custom_url_rewrite('alias', $result, $path);
  96. }
  97. return $result;
  98. }
  99. /**
  100. * Given a path alias, return the internal path it represents.
  101. *
  102. * @param $path
  103. * A Drupal path alias.
  104. *
  105. * @return
  106. * The internal path represented by the alias, or the original alias if no
  107. * internal path was found.
  108. */
  109. function drupal_get_normal_path($path) {
  110. $result = $path;
  111. if ($src = drupal_lookup_path('source', $path)) {
  112. $result = $src;
  113. }
  114. if (function_exists('custom_url_rewrite')) {
  115. $result = custom_url_rewrite('source', $result, $path);
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Return a component of the current Drupal path.
  121. *
  122. * When viewing a page at the path "admin/content/types", for example, arg(0)
  123. * would return "admin", arg(1) would return "content", and arg(2) would return
  124. * "types".
  125. *
  126. * Avoid use of this function where possible, as resulting code is hard to read.
  127. * Instead, attempt to use named arguments in menu callback functions. See the
  128. * explanation in menu.inc for how to construct callbacks that take arguments.
  129. *
  130. * @param $index
  131. * The index of the component, where each component is separated by a '/'
  132. * (forward-slash), and where the first component has an index of 0 (zero).
  133. *
  134. * @return
  135. * The component specified by $index, or FALSE if the specified component was
  136. * not found.
  137. */
  138. function arg($index) {
  139. static $arguments, $q;
  140. if (empty($arguments) || $q != $_GET['q']) {
  141. $arguments = explode('/', $_GET['q']);
  142. $q = $_GET['q'];
  143. }
  144. if (isset($arguments[$index])) {
  145. return $arguments[$index];
  146. }
  147. }
  148. /**
  149. * Get the title of the current page, for display on the page and in the title bar.
  150. *
  151. * @return
  152. * The current page's title.
  153. */
  154. function drupal_get_title() {
  155. $title = drupal_set_title();
  156. // during a bootstrap, menu.inc is not included and thus we cannot provide a title
  157. if (!isset($title) && function_exists('menu_get_active_title')) {
  158. $title = check_plain(menu_get_active_title());
  159. }
  160. return $title;
  161. }
  162. /**
  163. * Set the title of the current page, for display on the page and in the title bar.
  164. *
  165. * @param $title
  166. * Optional string value to assign to the page title; or if set to NULL
  167. * (default), leaves the current title unchanged.
  168. *
  169. * @return
  170. * The updated title of the current page.
  171. */
  172. function drupal_set_title($title = NULL) {
  173. static $stored_title;
  174. if (isset($title)) {
  175. $stored_title = $title;
  176. }
  177. return $stored_title;
  178. }
  179. /**
  180. * Check if the current page is the front page.
  181. *
  182. * @return
  183. * Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
  184. */
  185. function drupal_is_front_page() {
  186. // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
  187. // we can check it against the 'site_frontpage' variable.
  188. return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  189. }
Login or register to post comments