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