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

File

modules/path/path.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to rename URLs.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function path_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#path':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Path module allows you to specify an alias, or custom URL, for any existing internal system path. Aliases should not be confused with URL redirects, which allow you to forward a changed or inactive URL to a new URL. In addition to making URLs more readable, aliases also help search engines index content more effectively. Multiple aliases may be used for a single internal system path. To automate the aliasing of paths, you can install the contributed module <a href="@pathauto">Pathauto</a>. For more information, see the online handbook entry for the <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/documentation/modules/path', '@pathauto' => 'http://drupal.org/project/pathauto')) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Creating aliases') . '</dt>';
  18. $output .= '<dd>' . t('Users with sufficient <a href="@permissions">permissions</a> can create aliases under the <em>URL path settings</em> section when they create or edit content. Some examples of aliases are: ', array('@permissions' => url('admin/people/permissions', array('fragment' => 'module-path'))));
  19. $output .= '<ul><li>' . t('<em>member/jane-smith</em> aliased to internal path <em>user/123</em>') . '</li>';
  20. $output .= '<li>' . t('<em>about-us/team</em> aliased to internal path <em>node/456</em>') . '</li>';
  21. $output .= '</ul></dd>';
  22. $output .= '<dt>' . t('Managing aliases') . '</dt>';
  23. $output .= '<dd>' . t('The Path module provides a way to search and view a <a href="@aliases">list of all aliases</a> that are in use on your website. Aliases can be added, edited and deleted through this list.', array('@aliases' => url('admin/config/search/path'))) . '</dd>';
  24. $output .= '</dl>';
  25. return $output;
  26. case 'admin/config/search/path':
  27. return '<p>' . t("An alias defines a different name for an existing URL path - for example, the alias 'about' for the URL path 'node/1'. A URL path can have multiple aliases.") . '</p>';
  28. case 'admin/config/search/path/add':
  29. return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
  30. }
  31. }
  32. /**
  33. * Implements hook_permission().
  34. */
  35. function path_permission() {
  36. return array(
  37. 'administer url aliases' => array(
  38. 'title' => t('Administer URL aliases'),
  39. ),
  40. 'create url aliases' => array(
  41. 'title' => t('Create and edit URL aliases'),
  42. ),
  43. );
  44. }
  45. /**
  46. * Implements hook_menu().
  47. */
  48. function path_menu() {
  49. $items['admin/config/search/path'] = array(
  50. 'title' => 'URL aliases',
  51. 'description' => "Change your site's URL paths by aliasing them.",
  52. 'page callback' => 'path_admin_overview',
  53. 'access arguments' => array('administer url aliases'),
  54. 'weight' => -5,
  55. 'file' => 'path.admin.inc',
  56. );
  57. $items['admin/config/search/path/edit/%path'] = array(
  58. 'title' => 'Edit alias',
  59. 'page callback' => 'path_admin_edit',
  60. 'page arguments' => array(5),
  61. 'access arguments' => array('administer url aliases'),
  62. 'file' => 'path.admin.inc',
  63. );
  64. $items['admin/config/search/path/delete/%path'] = array(
  65. 'title' => 'Delete alias',
  66. 'page callback' => 'drupal_get_form',
  67. 'page arguments' => array('path_admin_delete_confirm', 5),
  68. 'access arguments' => array('administer url aliases'),
  69. 'file' => 'path.admin.inc',
  70. );
  71. $items['admin/config/search/path/list'] = array(
  72. 'title' => 'List',
  73. 'type' => MENU_DEFAULT_LOCAL_TASK,
  74. 'weight' => -10,
  75. );
  76. $items['admin/config/search/path/add'] = array(
  77. 'title' => 'Add alias',
  78. 'page callback' => 'path_admin_edit',
  79. 'access arguments' => array('administer url aliases'),
  80. 'type' => MENU_LOCAL_ACTION,
  81. 'file' => 'path.admin.inc',
  82. );
  83. return $items;
  84. }
  85. /**
  86. * Implements hook_form_BASE_FORM_ID_alter() for node_form().
  87. *
  88. * @see path_form_element_validate()
  89. */
  90. function path_form_node_form_alter(&$form, $form_state) {
  91. $path = array();
  92. if (!empty($form['#node']->nid)) {
  93. $conditions = array('source' => 'node/' . $form['#node']->nid);
  94. $langcode = entity_language('node', $form['#node']);
  95. if ($langcode != LANGUAGE_NONE) {
  96. $conditions['language'] = $langcode;
  97. }
  98. $path = path_load($conditions);
  99. if ($path === FALSE) {
  100. $path = array();
  101. }
  102. }
  103. $path += array(
  104. 'pid' => NULL,
  105. 'source' => isset($form['#node']->nid) ? 'node/' . $form['#node']->nid : NULL,
  106. 'alias' => '',
  107. 'language' => isset($langcode) ? $langcode : LANGUAGE_NONE,
  108. );
  109. $form['path'] = array(
  110. '#type' => 'fieldset',
  111. '#title' => t('URL path settings'),
  112. '#collapsible' => TRUE,
  113. '#collapsed' => empty($path['alias']),
  114. '#group' => 'additional_settings',
  115. '#attributes' => array(
  116. 'class' => array('path-form'),
  117. ),
  118. '#attached' => array(
  119. 'js' => array(drupal_get_path('module', 'path') . '/path.js'),
  120. ),
  121. '#access' => user_access('create url aliases') || user_access('administer url aliases'),
  122. '#weight' => 30,
  123. '#tree' => TRUE,
  124. '#element_validate' => array('path_form_element_validate'),
  125. );
  126. $form['path']['alias'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('URL alias'),
  129. '#default_value' => $path['alias'],
  130. '#maxlength' => 255,
  131. '#description' => t('Optionally specify an alternative URL by which this content 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.'),
  132. );
  133. $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
  134. $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
  135. $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
  136. }
  137. /**
  138. * Form element validation handler for URL alias form element.
  139. *
  140. * @see path_form_node_form_alter()
  141. */
  142. function path_form_element_validate($element, &$form_state, $complete_form) {
  143. // Trim the submitted value.
  144. $alias = trim($form_state['values']['path']['alias']);
  145. if (!empty($alias)) {
  146. form_set_value($element['alias'], $alias, $form_state);
  147. // Node language (Locale module) needs special care. Since the language of
  148. // the URL alias depends on the node language, and the node language can be
  149. // switched right within the same form, we need to conditionally overload
  150. // the originally assigned URL alias language.
  151. // @todo Remove this after converting Path module to a field, and, after
  152. // stopping Locale module from abusing the content language system.
  153. if (isset($form_state['values']['language'])) {
  154. form_set_value($element['language'], $form_state['values']['language'], $form_state);
  155. }
  156. $path = $form_state['values']['path'];
  157. // Ensure that the submitted alias does not exist yet.
  158. $query = db_select('url_alias')
  159. ->condition('alias', $path['alias'])
  160. ->condition('language', $path['language']);
  161. if (!empty($path['source'])) {
  162. $query->condition('source', $path['source'], '<>');
  163. }
  164. $query->addExpression('1');
  165. $query->range(0, 1);
  166. if ($query->execute()->fetchField()) {
  167. form_error($element, t('The alias is already in use.'));
  168. }
  169. }
  170. }
  171. /**
  172. * Implements hook_node_insert().
  173. */
  174. function path_node_insert($node) {
  175. if (isset($node->path)) {
  176. $path = $node->path;
  177. $path['alias'] = trim($path['alias']);
  178. // Only save a non-empty alias.
  179. if (!empty($path['alias'])) {
  180. // Ensure fields for programmatic executions.
  181. $langcode = entity_language('node', $node);
  182. $path['source'] = 'node/' . $node->nid;
  183. $path['language'] = isset($langcode) ? $langcode : LANGUAGE_NONE;
  184. path_save($path);
  185. }
  186. }
  187. }
  188. /**
  189. * Implements hook_node_update().
  190. */
  191. function path_node_update($node) {
  192. if (isset($node->path)) {
  193. $path = $node->path;
  194. $path['alias'] = trim($path['alias']);
  195. // Delete old alias if user erased it.
  196. if (!empty($path['pid']) && empty($path['alias'])) {
  197. path_delete($path['pid']);
  198. }
  199. path_node_insert($node);
  200. }
  201. }
  202. /**
  203. * Implements hook_node_delete().
  204. */
  205. function path_node_delete($node) {
  206. // Delete all aliases associated with this node.
  207. path_delete(array('source' => 'node/' . $node->nid));
  208. }
  209. /**
  210. * Implements hook_form_FORM_ID_alter() for taxonomy_form_term().
  211. */
  212. function path_form_taxonomy_form_term_alter(&$form, $form_state) {
  213. // Make sure this does not show up on the delete confirmation form.
  214. if (empty($form_state['confirm_delete'])) {
  215. $langcode = entity_language('taxonomy_term', (object) $form['#term']);
  216. $langcode = !empty($langcode) ? $langcode : LANGUAGE_NONE;
  217. $conditions = array('source' => 'taxonomy/term/' . $form['#term']['tid'], 'language' => $langcode);
  218. $path = (isset($form['#term']['tid']) ? path_load($conditions) : array());
  219. if ($path === FALSE) {
  220. $path = array();
  221. }
  222. $path += array(
  223. 'pid' => NULL,
  224. 'source' => isset($form['#term']['tid']) ? 'taxonomy/term/' . $form['#term']['tid'] : NULL,
  225. 'alias' => '',
  226. 'language' => $langcode,
  227. );
  228. $form['path'] = array(
  229. '#access' => user_access('create url aliases') || user_access('administer url aliases'),
  230. '#tree' => TRUE,
  231. '#element_validate' => array('path_form_element_validate'),
  232. );
  233. $form['path']['alias'] = array(
  234. '#type' => 'textfield',
  235. '#title' => t('URL alias'),
  236. '#default_value' => $path['alias'],
  237. '#maxlength' => 255,
  238. '#weight' => 0,
  239. '#description' => t("Optionally specify an alternative URL by which this term can be accessed. Use a relative path and don't add a trailing slash or the URL alias won't work."),
  240. );
  241. $form['path']['pid'] = array('#type' => 'value', '#value' => $path['pid']);
  242. $form['path']['source'] = array('#type' => 'value', '#value' => $path['source']);
  243. $form['path']['language'] = array('#type' => 'value', '#value' => $path['language']);
  244. }
  245. }
  246. /**
  247. * Implements hook_taxonomy_term_insert().
  248. */
  249. function path_taxonomy_term_insert($term) {
  250. if (isset($term->path)) {
  251. $path = $term->path;
  252. $path['alias'] = trim($path['alias']);
  253. // Only save a non-empty alias.
  254. if (!empty($path['alias'])) {
  255. // Ensure fields for programmatic executions.
  256. $path['source'] = 'taxonomy/term/' . $term->tid;
  257. // Core does not provide a way to store the term language but contrib
  258. // modules can do it so we need to take this into account.
  259. $langcode = entity_language('taxonomy_term', $term);
  260. $path['language'] = !empty($langcode) ? $langcode : LANGUAGE_NONE;
  261. path_save($path);
  262. }
  263. }
  264. }
  265. /**
  266. * Implements hook_taxonomy_term_update().
  267. */
  268. function path_taxonomy_term_update($term) {
  269. if (isset($term->path)) {
  270. $path = $term->path;
  271. $path['alias'] = trim($path['alias']);
  272. // Delete old alias if user erased it.
  273. if (!empty($path['pid']) && empty($path['alias'])) {
  274. path_delete($path['pid']);
  275. }
  276. // Only save a non-empty alias.
  277. if (!empty($path['alias'])) {
  278. // Ensure fields for programmatic executions.
  279. $path['source'] = 'taxonomy/term/' . $term->tid;
  280. // Core does not provide a way to store the term language but contrib
  281. // modules can do it so we need to take this into account.
  282. $langcode = entity_language('taxonomy_term', $term);
  283. $path['language'] = !empty($langcode) ? $langcode : LANGUAGE_NONE;
  284. path_save($path);
  285. }
  286. }
  287. }
  288. /**
  289. * Implements hook_taxonomy_term_delete().
  290. */
  291. function path_taxonomy_term_delete($term) {
  292. // Delete all aliases associated with this term.
  293. path_delete(array('source' => 'taxonomy/term/' . $term->tid));
  294. }