path.module

You are here

  1. 7 modules/path/path.module
  2. 4.6 modules/path.module
  3. 4.7 modules/path.module
  4. 5 modules/path/path.module
  5. 6 modules/path/path.module
  6. 8 core/modules/path/path.module

Enables users to rename URLs.

Functions

Namesort descending Description
path_admin_delete Post-confirmation; delete an URL alias.
path_form_alter Implementation of hook_form_alter().
path_help Implementation of hook_help().
path_load Fetch a specific URL alias from the database.
path_menu Implementation of hook_menu().
path_nodeapi Implementation of hook_nodeapi().
path_perm Implementation of hook_perm().
path_set_alias Set an aliased path for a given Drupal path, preventing duplicates.

File

modules/path/path.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to rename URLs.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function path_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#path':
  12. $output = '<p>'. t('The path module allows you to specify aliases for Drupal URLs. Such aliases improve readability of URLs for your users and may help internet search engines to index your content more effectively. More than one alias may be created for a given page.') .'</p>';
  13. $output .= t('<p>Some examples of URL aliases are:</p>
  14. <ul>
  15. <li>user/login =&gt; login</li>
  16. <li>image/tid/16 =&gt; store</li>
  17. <li>taxonomy/term/7+19+20+21 =&gt; store/products/whirlygigs</li>
  18. <li>node/3 =&gt; contact</li>
  19. </ul>
  20. ');
  21. $output .= '<p>'. t('The path module enables appropriately permissioned users to specify an optional alias in all node input and editing forms, and provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are <em>administer url aliases</em> and <em>create url aliases</em>. ') .'</p>';
  22. $output .= '<p>'. t('This module also provides user-defined mass URL aliasing capabilities, which is useful if you wish to uniformly use URLs different from the default. For example, you may want to have your URLs presented in a different language. Access to the Drupal source code on the web server is required to set up mass URL aliasing. ') .'</p>';
  23. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
  24. return $output;
  25. case 'admin/build/path':
  26. return '<p>'. t("Drupal provides complete control over URLs through aliasing, which is often used to make URLs more readable or easy to remember. For example, the alias 'about' may be mapped onto the post at the system path 'node/1', creating a more meaningful URL. Each system path can have multiple aliases.") .'</p>';
  27. case 'admin/build/path/add':
  28. return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>';
  29. }
  30. }
  31. /**
  32. * Implementation of hook_menu().
  33. */
  34. function path_menu() {
  35. $items['admin/build/path'] = array(
  36. 'title' => 'URL aliases',
  37. 'description' => "Change your site's URL paths by aliasing them.",
  38. 'page callback' => 'path_admin_overview',
  39. 'access arguments' => array('administer url aliases'),
  40. 'file' => 'path.admin.inc',
  41. );
  42. $items['admin/build/path/edit'] = array(
  43. 'title' => 'Edit alias',
  44. 'page callback' => 'path_admin_edit',
  45. 'access arguments' => array('administer url aliases'),
  46. 'type' => MENU_CALLBACK,
  47. 'file' => 'path.admin.inc',
  48. );
  49. $items['admin/build/path/delete'] = array(
  50. 'title' => 'Delete alias',
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('path_admin_delete_confirm'),
  53. 'access arguments' => array('administer url aliases'),
  54. 'type' => MENU_CALLBACK,
  55. 'file' => 'path.admin.inc',
  56. );
  57. $items['admin/build/path/list'] = array(
  58. 'title' => 'List',
  59. 'type' => MENU_DEFAULT_LOCAL_TASK,
  60. 'weight' => -10,
  61. );
  62. $items['admin/build/path/add'] = array(
  63. 'title' => 'Add alias',
  64. 'page callback' => 'path_admin_edit',
  65. 'access arguments' => array('administer url aliases'),
  66. 'type' => MENU_LOCAL_TASK,
  67. 'file' => 'path.admin.inc',
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Post-confirmation; delete an URL alias.
  73. */
  74. function path_admin_delete($pid = 0) {
  75. db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  76. drupal_set_message(t('The alias has been deleted.'));
  77. }
  78. /**
  79. * Set an aliased path for a given Drupal path, preventing duplicates.
  80. *
  81. * @param $path
  82. * Path URL. Set to NULL to delete alias.
  83. * @param $alias
  84. * Alias URL. Set to NULL to delete alias.
  85. * @param $pid
  86. * Path id to update. Set to NULL to create a new alias or to delete a group of aliases.
  87. * @param $language
  88. * The language this alias is valid for.
  89. */
  90. function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
  91. /* This function claimed to prevent duplicate aliases but has not done
  92. * so since the end of 2007.
  93. * The uniqueness of dst+language pairs was enforced on the database level
  94. * until D6.16 (march 2010); trying to insert duplicate aliass would yield a
  95. * database error.
  96. * From D6.16 onwards, duplicates would silently be inserted, and
  97. * drupal_lookup_path() consistently uses those newer aliases.
  98. * While fixing an issue in D6.23, the behavior was reverted to preventing
  99. * duplicates by the below code. Watchdog errors are now logged instead.
  100. */
  101. $path = urldecode($path);
  102. $alias = urldecode($alias);
  103. // First we check if we deal with an existing alias and delete or modify it based on pid.
  104. if ($pid) {
  105. // An existing alias.
  106. if (!$path || !$alias) {
  107. // Delete the alias based on pid.
  108. db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  109. }
  110. else {
  111. // Update the existing alias.
  112. // Check if the alias exists already.
  113. $existing = db_fetch_array(db_query("SELECT pid, src FROM {url_alias} WHERE dst = '%s' AND language = '%s' ORDER BY pid DESC", $alias, $language));
  114. if (!$existing || ($existing['pid'] == $pid && $existing['src'] != $path)) {
  115. db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
  116. }
  117. else {
  118. if ($existing['src'] != $path) {
  119. watchdog('path', "The alias for path '@path' (language '@lang') was not updated to '@alias', because the path '@expath' already has the same alias.",
  120. array('@path' => $path, '@lang' => $language, '@alias' => $alias, '@expath' => $existing['src']),
  121. WATCHDOG_ERROR);
  122. }
  123. // Don't clear cache if we didn't change anything
  124. return;
  125. }
  126. }
  127. }
  128. elseif ($path && $alias) {
  129. // Add this alias to the database, if it's new & doesn't cause conflicts.
  130. $existing = db_fetch_array(db_query("SELECT src, language, pid FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $alias, $language));
  131. if (!$existing || ($existing['language'] != $language && $existing['src'] != $path)) {
  132. // A new alias. Add it to the database.
  133. db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
  134. }
  135. elseif ($existing['language'] != $language) {
  136. // This alias already exists ONLY for 'language neutral': update language.
  137. // (We can only get here if $language != '')
  138. db_query("UPDATE {url_alias} SET language = '%s' WHERE pid = %d", $language, $existing['pid']);
  139. }
  140. else {
  141. if ($existing['src'] != $path) {
  142. watchdog('path', "The alias '@alias' for path '@path' (language '@lang') was not created, because the path '@expath' already has the same alias.",
  143. array('@path' => $path, '@lang' => $language, '@alias' => $alias, '@expath' => $existing['src']),
  144. WATCHDOG_ERROR);
  145. }
  146. // Don't clear cache if we didn't change anything
  147. return;
  148. }
  149. }
  150. else {
  151. // Delete the alias.
  152. if ($alias) {
  153. db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
  154. }
  155. else {
  156. db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
  157. }
  158. }
  159. drupal_clear_path_cache();
  160. }
  161. /**
  162. * Implementation of hook_nodeapi().
  163. *
  164. * Allows URL aliases for nodes to be specified at node edit time rather
  165. * than through the administrative interface.
  166. */
  167. function path_nodeapi(&$node, $op, $arg = NULL) {
  168. // Permissions are required for everything except node loading.
  169. if (user_access('create url aliases') || user_access('administer url aliases') || ($op == 'load')) {
  170. $language = isset($node->language) ? $node->language : '';
  171. switch ($op) {
  172. case 'validate':
  173. if (isset($node->path)) {
  174. $node->path = trim($node->path);
  175. if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s' AND language = '%s'", $node->path, "node/$node->nid", $language))) {
  176. form_set_error('path', t('The path is already in use.'));
  177. }
  178. }
  179. break;
  180. case 'load':
  181. $path = 'node/'. $node->nid;
  182. $alias = drupal_get_path_alias($path, $language);
  183. if ($path != $alias) {
  184. $node->path = $alias;
  185. }
  186. break;
  187. case 'insert':
  188. // Don't try to insert if path is NULL. We may have already set
  189. // the alias ahead of time.
  190. if (isset($node->path)) {
  191. path_set_alias('node/'. $node->nid, $node->path, NULL, $language);
  192. }
  193. break;
  194. case 'update':
  195. // $node->pid is usually only set when updating from a node edit form
  196. // (see path_form_alter). If it is not set (e.g. on most node_save()
  197. // commands), we cannot be sure whether a change in $node->path is meant
  198. // to replace an existing alias or add one extra, so we do the latter.
  199. path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
  200. break;
  201. case 'delete':
  202. $path = 'node/'. $node->nid;
  203. if (drupal_get_path_alias($path) != $path) {
  204. path_set_alias($path);
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. /**
  211. * Implementation of hook_form_alter().
  212. */
  213. function path_form_alter(&$form, $form_state, $form_id) {
  214. if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
  215. $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
  216. $form['path'] = array(
  217. '#type' => 'fieldset',
  218. '#title' => t('URL path settings'),
  219. '#collapsible' => TRUE,
  220. '#collapsed' => empty($path),
  221. '#access' => user_access('create url aliases'),
  222. '#weight' => 30,
  223. );
  224. $form['path']['path'] = array(
  225. '#type' => 'textfield',
  226. '#default_value' => $path,
  227. '#maxlength' => 128,
  228. '#collapsible' => TRUE,
  229. '#collapsed' => TRUE,
  230. '#description' => t('Optionally specify an alternative URL by which this node can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
  231. );
  232. if ($path) {
  233. $form['path']['pid'] = array(
  234. '#type' => 'value',
  235. '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
  236. );
  237. }
  238. }
  239. }
  240. /**
  241. * Implementation of hook_perm().
  242. */
  243. function path_perm() {
  244. return array('create url aliases', 'administer url aliases');
  245. }
  246. /**
  247. * Fetch a specific URL alias from the database.
  248. */
  249. function path_load($pid) {
  250. return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
  251. }