pathauto.module

Tracking 7.x-1.x branch
  1. drupal
    1. 5 contributions/pathauto/pathauto.module
    2. 6 contributions/pathauto/pathauto.module
    3. 7 contributions/pathauto/pathauto.module

Main file for the Pathauto module, which automatically generates aliases for content.

Functions & methods

NameDescription
pathauto_action_infoImplements hook_action_info().
pathauto_blog_update_aliasUpdate the blog URL aliases for an individual user account.
pathauto_entity_languageReturns the language code of the given entity.
pathauto_entity_path_delete_allDelete an entity URL alias and any of its sub-paths.
pathauto_entity_presaveImplements hook_entity_presave().
pathauto_field_attach_delete_bundleImplements hook_field_attach_delete_bundle().
pathauto_field_attach_formImplements hook_field_attach_form().
pathauto_field_attach_rename_bundleImplements hook_field_attach_rename_bundle().
pathauto_form_node_form_alterImplements hook_form_BASE_FORM_ID_alter().
pathauto_form_taxonomy_form_term_alterImplements hook_form_FORM_ID_alter().
pathauto_helpImplements hook_help().
pathauto_hook_infoImplements hook_hook_info().
pathauto_menuImplements hook_menu().
pathauto_module_implements_alterImplements hook_module_implements_alter().
pathauto_node_deleteImplements hook_node_delete().
pathauto_node_insertImplements hook_node_insert().
pathauto_node_operationsImplements hook_node_operations().
pathauto_node_updateImplements hook_node_update().
pathauto_node_update_actionUpdate action wrapper for pathauto_node_update_alias().
pathauto_node_update_aliasUpdate the URL aliases for an individual node.
pathauto_node_update_alias_multipleUpdate the URL aliases for multiple nodes.
pathauto_path_delete_allDelete an URL alias and any of its sub-paths.
pathauto_path_delete_multipleDelete multiple URL aliases.
pathauto_pattern_load_by_entityLoad an URL alias pattern by entity, bundle, and language.
pathauto_permissionImplements hook_permission().
pathauto_taxonomy_term_deleteImplements hook_taxonomy_term_delete().
pathauto_taxonomy_term_insertImplements hook_taxonomy_term_insert().
pathauto_taxonomy_term_updateImplements hook_taxonomy_term_update().
pathauto_taxonomy_term_update_actionUpdate action wrapper for pathauto_taxonomy_term_update_alias().
pathauto_taxonomy_term_update_aliasUpdate the URL aliases for an individual taxonomy term.
pathauto_taxonomy_term_update_alias_multipleUpdate the URL aliases for multiple taxonomy terms.
pathauto_user_deleteImplements hook_user_delete().
pathauto_user_insertImplements hook_user_insert().
pathauto_user_operationsImplements hook_user_operations().
pathauto_user_updateImplements hook_user_update().
pathauto_user_update_actionUpdate action wrapper for pathauto_user_update_alias().
pathauto_user_update_aliasUpdate the URL aliases for an individual user account.
pathauto_user_update_alias_multipleUpdate the URL aliases for multiple user accounts.

Constants

NameDescription
PATHAUTO_IGNORE_WORDSThe default ignore word list.

File

View source
  1. <?php
  2. /**
  3. * @defgroup pathauto Pathauto: Automatically generates aliases for content
  4. *
  5. * The Pathauto module automatically generates path aliases for various kinds of
  6. * content (nodes, categories, users) without requiring the user to manually
  7. * specify the path alias. This allows you to get aliases like
  8. * /category/my-node-title.html instead of /node/123. The aliases are based upon
  9. * a "pattern" system which the administrator can control.
  10. */
  11. /**
  12. * @file
  13. * Main file for the Pathauto module, which automatically generates aliases for content.
  14. *
  15. * @ingroup pathauto
  16. */
  17. /**
  18. * The default ignore word list.
  19. */
  20. define('PATHAUTO_IGNORE_WORDS', 'a, an, as, at, before, but, by, for, from, is, in, into, like, of, off, on, onto, per, since, than, the, this, that, to, up, via, with');
  21. /**
  22. * Implements hook_hook_info().
  23. */
  24. function pathauto_hook_info() {
  25. $info['pathauto'] = array('group' => 'pathauto');
  26. $info['path_alias_types'] = array('group' => 'pathauto');
  27. return $info;
  28. }
  29. /**
  30. * Implements hook_module_implements_alter().
  31. *
  32. * Adds pathauto support for core modules.
  33. */
  34. function pathauto_module_implements_alter(&$implementations, $hook) {
  35. $hooks = pathauto_hook_info();
  36. if (isset($hooks[$hook])) {
  37. $modules = array('node', 'taxonomy', 'user', 'forum', 'blog');
  38. foreach ($modules as $module) {
  39. if (module_exists($module)) {
  40. $implementations[$module] = TRUE;
  41. }
  42. }
  43. // Move pathauto.module to get included first since it is responsible for
  44. // other modules.
  45. unset($implementations['pathauto']);
  46. $implementations = array_merge(array('pathauto' => 'pathauto'), $implementations);
  47. }
  48. }
  49. /**
  50. * Implements hook_help().
  51. */
  52. function pathauto_help($path, $arg) {
  53. switch ($path) {
  54. case 'admin/help#pathauto':
  55. module_load_include('inc', 'pathauto');
  56. $output = '<h3>' . t('About') . '</h3>';
  57. $output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
  58. $output .= '<h3>' . t('Settings') . '</h3>';
  59. $output .= '<dl>';
  60. $output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
  61. $output .= '<dd>' . t('The <strong>maximum alias length</strong> and <strong>maximum component length</strong> values default to 100 and have a limit of @max from Pathauto. This length is limited by the length of the "alias" column of the url_alias database table. The default database schema for this column is @max. If you set a length that is equal to that of the one set in the "alias" column it will cause problems in situations where the system needs to append additional words to the aliased URL. You should enter a value that is the length of the "alias" column minus the length of any strings that might get added to the end of the URL. The length of strings that might get added to the end of your URLs depends on which modules you have enabled and on your Pathauto settings. The recommended and default value is 100.', array('@max' => _pathauto_get_schema_alias_maxlength())) . '</dd>';
  62. $output .= '</dl>';
  63. return $output;
  64. }
  65. }
  66. /**
  67. * Implements hook_permission().
  68. */
  69. function pathauto_permission() {
  70. return array(
  71. 'administer pathauto' => array(
  72. 'title' => t('Administer pathauto'),
  73. 'description' => t('Allows a user to configure patterns for automated aliases and bulk delete URL-aliases.'),
  74. ),
  75. 'notify of path changes' => array(
  76. 'title' => t('Notify of Path Changes'),
  77. 'description' => t('Determines whether or not users are notified.'),
  78. ),
  79. );
  80. }
  81. /**
  82. * Implements hook_menu().
  83. */
  84. function pathauto_menu() {
  85. $items['admin/config/search/path/patterns'] = array(
  86. 'title' => 'Patterns',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('pathauto_patterns_form'),
  89. 'access arguments' => array('administer pathauto'),
  90. 'type' => MENU_LOCAL_TASK,
  91. 'weight' => 10,
  92. 'file' => 'pathauto.admin.inc',
  93. );
  94. $items['admin/config/search/path/settings'] = array(
  95. 'title' => 'Settings',
  96. 'page callback' => 'drupal_get_form',
  97. 'page arguments' => array('pathauto_settings_form'),
  98. 'access arguments' => array('administer pathauto'),
  99. 'type' => MENU_LOCAL_TASK,
  100. 'weight' => 20,
  101. 'file' => 'pathauto.admin.inc',
  102. );
  103. $items['admin/config/search/path/update_bulk'] = array(
  104. 'title' => 'Bulk update',
  105. 'page callback' => 'drupal_get_form',
  106. 'page arguments' => array('pathauto_bulk_update_form'),
  107. 'access arguments' => array('administer url aliases'),
  108. 'type' => MENU_LOCAL_TASK,
  109. 'weight' => 30,
  110. 'file' => 'pathauto.admin.inc',
  111. );
  112. $items['admin/config/search/path/delete_bulk'] = array(
  113. 'title' => 'Delete aliases',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('pathauto_admin_delete'),
  116. 'access arguments' => array('administer url aliases'),
  117. 'type' => MENU_LOCAL_TASK,
  118. 'weight' => 40,
  119. 'file' => 'pathauto.admin.inc',
  120. );
  121. return $items;
  122. }
  123. /**
  124. * Load an URL alias pattern by entity, bundle, and language.
  125. *
  126. * @param $entity
  127. * An entity (e.g. node, taxonomy, user, etc.)
  128. * @param $bundle
  129. * A bundle (e.g. content type, vocabulary ID, etc.)
  130. * @param $language
  131. * A language code, defaults to the LANGUAGE_NONE constant.
  132. */
  133. function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = LANGUAGE_NONE) {
  134. $patterns = &drupal_static(__FUNCTION__, array());
  135. $pattern_id = "$entity:$bundle:$language";
  136. if (!isset($patterns[$pattern_id])) {
  137. $variables = array();
  138. if ($language != LANGUAGE_NONE) {
  139. $variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern";
  140. }
  141. if ($bundle) {
  142. $variables[] = "pathauto_{$entity}_{$bundle}_pattern";
  143. }
  144. $variables[] = "pathauto_{$entity}_pattern";
  145. foreach ($variables as $variable) {
  146. if ($pattern = trim(variable_get($variable, ''))) {
  147. break;
  148. }
  149. }
  150. $patterns[$pattern_id] = $pattern;
  151. }
  152. return $patterns[$pattern_id];
  153. }
  154. /**
  155. * Delete multiple URL aliases.
  156. *
  157. * Intent of this is to abstract a potential path_delete_multiple() function
  158. * for Drupal 7 or 8.
  159. *
  160. * @param $pids
  161. * An array of path IDs to delete.
  162. */
  163. function pathauto_path_delete_multiple($pids) {
  164. foreach ($pids as $pid) {
  165. path_delete(array('pid' => $pid));
  166. }
  167. }
  168. /**
  169. * Delete an URL alias and any of its sub-paths.
  170. *
  171. * Given a source like 'node/1' this function will delete any alias that have
  172. * that specific source or any sources that match 'node/1/%'.
  173. *
  174. * @param $source
  175. * An string with a source URL path.
  176. */
  177. function pathauto_path_delete_all($source) {
  178. $sql = "SELECT pid FROM {url_alias} WHERE source = :source OR source LIKE :source_wildcard";
  179. $pids = db_query($sql, array(':source' => $source, ':source_wildcard' => $source . '/%'))->fetchCol();
  180. if ($pids) {
  181. pathauto_path_delete_multiple($pids);
  182. }
  183. }
  184. /**
  185. * Delete an entity URL alias and any of its sub-paths.
  186. *
  187. * This function also checks to see if the default entity URI is different from
  188. * the current entity URI and will delete any of the default aliases.
  189. *
  190. * @param $entity_type
  191. * A string with the entity type.
  192. * @param $entity
  193. * An entity object.
  194. * @param $default_uri
  195. * The optional default uri path for the entity.
  196. */
  197. function pathauto_entity_path_delete_all($entity_type, $entity, $default_uri = NULL) {
  198. $uri = entity_uri($entity_type, $entity);
  199. pathauto_path_delete_all($uri['path']);
  200. if (isset($default_uri) && $uri['path'] != $default_uri) {
  201. pathauto_path_delete_all($default_uri);
  202. }
  203. }
  204. /**
  205. * Implements hook_field_attach_rename_bundle().
  206. *
  207. * Respond to machine name changes for pattern variables.
  208. */
  209. function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  210. $variables = db_select('variable', 'v')
  211. ->fields('v', array('name'))
  212. ->condition('name', db_like("pathauto_{$entity_type}_{$bundle_old}_") . '%', 'LIKE')
  213. ->execute()
  214. ->fetchCol();
  215. foreach ($variables as $variable) {
  216. $value = variable_get($variable, '');
  217. variable_del($variable);
  218. $variable = strtr($variable, array("{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}"));
  219. variable_set($variable, $value);
  220. }
  221. }
  222. /**
  223. * Implements hook_field_attach_delete_bundle().
  224. *
  225. * Respond to sub-types being deleted, their patterns can be removed.
  226. */
  227. function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
  228. $variables = db_select('variable', 'v')
  229. ->fields('v', array('name'))
  230. ->condition('name', db_like("pathauto_{$entity_type}_{$bundle}_") . '%', 'LIKE')
  231. ->execute()
  232. ->fetchCol();
  233. foreach ($variables as $variable) {
  234. variable_del($variable);
  235. }
  236. }
  237. /**
  238. * Implements hook_field_attach_form().
  239. *
  240. * Add the automatic alias form elements to an existing path form fieldset.
  241. */
  242. function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  243. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  244. if (!isset($form['path'])) {
  245. // This entity must be supported by core's path.module first.
  246. // @todo Investigate removing this and supporting all fieldable entities.
  247. return;
  248. }
  249. else {
  250. // Taxonomy terms do not have an actual fieldset for path settings.
  251. // Merge in the defaults.
  252. $form['path'] += array(
  253. '#type' => 'fieldset',
  254. '#title' => t('URL path settings'),
  255. '#collapsible' => TRUE,
  256. '#collapsed' => empty($form['path']['alias']),
  257. '#group' => 'additional_settings',
  258. '#attributes' => array(
  259. 'class' => array('path-form'),
  260. ),
  261. '#access' => user_access('create url aliases') || user_access('administer url aliases'),
  262. '#weight' => 30,
  263. '#tree' => TRUE,
  264. '#element_validate' => array('path_form_element_validate'),
  265. );
  266. }
  267. $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $langcode);
  268. if (empty($pattern)) {
  269. return;
  270. }
  271. if (!isset($entity->path['pathauto'])) {
  272. if (!empty($id)) {
  273. module_load_include('inc', 'pathauto');
  274. $uri = entity_uri($entity_type, $entity);
  275. $path = drupal_get_path_alias($uri['path'], $langcode);
  276. $pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array($entity_type => $entity), $bundle, $langcode);
  277. $entity->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);
  278. }
  279. else {
  280. $entity->path['pathauto'] = TRUE;
  281. }
  282. }
  283. // Add JavaScript that will disable the path textfield when the automatic
  284. // alias checkbox is checked.
  285. $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]'] = array('checked' => TRUE);
  286. // Override path.module's vertical tabs summary.
  287. $form['path']['#attached']['js'] = array(
  288. 'vertical-tabs' => drupal_get_path('module', 'pathauto') . '/pathauto.js'
  289. );
  290. $form['path']['pathauto'] = array(
  291. '#type' => 'checkbox',
  292. '#title' => t('Generate automatic URL alias'),
  293. '#default_value' => $entity->path['pathauto'],
  294. '#description' => t('Uncheck this to create a custom alias below.'),
  295. '#weight' => -1,
  296. );
  297. // Add a shortcut link to configure URL alias patterns.
  298. if (drupal_valid_path('admin/config/search/path/patterns')) {
  299. $form['path']['pathauto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/search/path/patterns');
  300. }
  301. if ($entity->path['pathauto'] && !empty($entity->old_alias) && empty($entity->path['alias'])) {
  302. $form['path']['alias']['#default_value'] = $entity->old_alias;
  303. $entity->path['alias'] = $entity->old_alias;
  304. }
  305. // For Pathauto to remember the old alias and prevent the Path module from
  306. // deleting it when Pathauto wants to preserve it.
  307. if (!empty($entity->path['alias'])) {
  308. $form['path']['old_alias'] = array(
  309. '#type' => 'value',
  310. '#value' => $entity->path['alias'],
  311. );
  312. }
  313. }
  314. /**
  315. * Implements hook_entity_presave().
  316. */
  317. function pathauto_entity_presave($entity, $type) {
  318. // About to be saved (before insert/update)
  319. if (!empty($entity->path['pathauto']) && isset($entity->path['old_alias'])
  320. && $entity->path['alias'] == '' && $entity->path['old_alias'] != '') {
  321. /**
  322. * There was an old alias, but when pathauto_perform_alias was checked
  323. * the javascript disabled the textbox which led to an empty value being
  324. * submitted. Restoring the old path-value here prevents the Path module
  325. * from deleting any old alias before Pathauto gets control.
  326. */
  327. $entity->path['alias'] = $entity->path['old_alias'];
  328. }
  329. // Help prevent errors with progromatically creating entities by defining
  330. // path['alias'] as an empty string.
  331. // @see http://drupal.org/node/1328180
  332. // @see http://drupal.org/node/1576552
  333. if (isset($entity->path['pathauto']) && !isset($entity->path['alias'])) {
  334. $entity->path['alias'] = '';
  335. }
  336. }
  337. /**
  338. * Implements hook_action_info().
  339. */
  340. function pathauto_action_info() {
  341. $info['pathauto_node_update_action'] = array(
  342. 'type' => 'node',
  343. 'label' => t('Update node alias'),
  344. 'configurable' => FALSE,
  345. 'triggers' => array(),
  346. );
  347. $info['pathauto_taxonomy_term_update_action'] = array(
  348. 'type' => 'taxonomy_term',
  349. 'label' => t('Update taxonomy term alias'),
  350. 'configurable' => FALSE,
  351. 'triggers' => array(),
  352. );
  353. $info['pathauto_user_update_action'] = array(
  354. 'type' => 'user',
  355. 'label' => t('Update user alias'),
  356. 'configurable' => FALSE,
  357. 'triggers' => array(),
  358. );
  359. return $info;
  360. }
  361. /**
  362. * Returns the language code of the given entity.
  363. *
  364. * Backward compatibility layer to ensure that installations running an older
  365. * version of core where entity_language() is not avilable do not break.
  366. *
  367. * @param string $entity_type
  368. * An entity type.
  369. * @param object $entity
  370. * An entity object.
  371. * @param bool $check_language_property
  372. * A boolean if TRUE, will attempt to fetch the language code from
  373. * $entity->language if the entity_language() function failed or does not
  374. * exist. Default is TRUE.
  375. */
  376. function pathauto_entity_language($entity_type, $entity, $check_language_property = TRUE) {
  377. $langcode = NULL;
  378. if (function_exists('entity_language')) {
  379. $langcode = entity_language($entity_type, $entity);
  380. }
  381. elseif ($check_language_property && !empty($entity->language)) {
  382. $langcode = $entity->language;
  383. }
  384. return !empty($langcode) ? $langcode : LANGUAGE_NONE;
  385. }
  386. if (!function_exists('path_field_extra_fields')) {
  387. /**
  388. * Implements hook_field_extra_fields() on behalf of path.module.
  389. *
  390. * Add support for the 'URL path settings' to be re-ordered by the user on the
  391. * 'Manage Fields' tab of content types and vocabularies.
  392. */
  393. function path_field_extra_fields() {
  394. $info = array();
  395. foreach (node_type_get_types() as $node_type) {
  396. if (!isset($info['node'][$node_type->type]['form']['path'])) {
  397. $info['node'][$node_type->type]['form']['path'] = array(
  398. 'label' => t('URL path settings'),
  399. 'description' => t('Path module form elements'),
  400. 'weight' => 30,
  401. );
  402. }
  403. }
  404. if (module_exists('taxonomy')) {
  405. $vocabularies = taxonomy_get_vocabularies();
  406. foreach ($vocabularies as $vocabulary) {
  407. if (!isset($info['taxonomy_term'][$vocabulary->machine_name]['form']['path'])) {
  408. $info['taxonomy_term'][$vocabulary->machine_name]['form']['path'] = array(
  409. 'label' => t('URL path settings'),
  410. 'description' => t('Path module form elements'),
  411. 'weight' => 30,
  412. );
  413. }
  414. }
  415. }
  416. return $info;
  417. }
  418. }
  419. /**
  420. * @name pathauto_node Pathauto integration for the core node module.
  421. * @{
  422. */
  423. /**
  424. * Implements hook_node_insert().
  425. */
  426. function pathauto_node_insert($node) {
  427. // @todo Remove the next line when http://drupal.org/node/1025870 is fixed.
  428. unset($node->uri);
  429. pathauto_node_update_alias($node, 'insert');
  430. }
  431. /**
  432. * Implements hook_node_update().
  433. */
  434. function pathauto_node_update($node) {
  435. pathauto_node_update_alias($node, 'update');
  436. }
  437. /**
  438. * Implements hook_node_delete().
  439. */
  440. function pathauto_node_delete($node) {
  441. pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}");
  442. }
  443. /**
  444. * Implements hook_form_BASE_FORM_ID_alter().
  445. *
  446. * Add the Pathauto settings to the node form.
  447. */
  448. function pathauto_form_node_form_alter(&$form, &$form_state) {
  449. $node = $form_state['node'];
  450. $langcode = pathauto_entity_language('node', $node);
  451. pathauto_field_attach_form('node', $node, $form, $form_state, $langcode);
  452. }
  453. /**
  454. * Implements hook_node_operations().
  455. */
  456. function pathauto_node_operations() {
  457. $operations['pathauto_update_alias'] = array(
  458. 'label' => t('Update URL alias'),
  459. 'callback' => 'pathauto_node_update_alias_multiple',
  460. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  461. );
  462. return $operations;
  463. }
  464. /**
  465. * Update the URL aliases for an individual node.
  466. *
  467. * @param $node
  468. * A node object.
  469. * @param $op
  470. * Operation being performed on the node ('insert', 'update' or 'bulkupdate').
  471. * @param $options
  472. * An optional array of additional options.
  473. */
  474. function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) {
  475. // Skip processing if the user has disabled pathauto for the node.
  476. if (isset($node->path['pathauto']) && empty($node->path['pathauto'])) {
  477. return;
  478. }
  479. $options += array('language' => pathauto_entity_language('node', $node));
  480. // Skip processing if the node has no pattern.
  481. if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
  482. return;
  483. }
  484. module_load_include('inc', 'pathauto');
  485. $uri = entity_uri('node', $node);
  486. pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']);
  487. }
  488. /**
  489. * Update the URL aliases for multiple nodes.
  490. *
  491. * @param $nids
  492. * An array of node IDs.
  493. * @param $op
  494. * Operation being performed on the nodes ('insert', 'update' or
  495. * 'bulkupdate').
  496. * @param $options
  497. * An optional array of additional options.
  498. */
  499. function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) {
  500. $options += array('message' => FALSE);
  501. $nodes = node_load_multiple($nids);
  502. foreach ($nodes as $node) {
  503. pathauto_node_update_alias($node, $op, $options);
  504. }
  505. if (!empty($options['message'])) {
  506. drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
  507. }
  508. }
  509. /**
  510. * Update action wrapper for pathauto_node_update_alias().
  511. */
  512. function pathauto_node_update_action($node, $context = array()) {
  513. pathauto_node_update_alias($node, 'bulkupdate', array('message' => TRUE));
  514. }
  515. /**
  516. * @} End of "name pathauto_node".
  517. */
  518. /**
  519. * @name pathauto_taxonomy Pathauto integration for the core taxonomy module.
  520. * @{
  521. */
  522. /**
  523. * Implements hook_taxonomy_term_insert().
  524. */
  525. function pathauto_taxonomy_term_insert($term) {
  526. pathauto_taxonomy_term_update_alias($term, 'insert');
  527. }
  528. /**
  529. * Implements hook_taxonomy_term_update().
  530. */
  531. function pathauto_taxonomy_term_update($term) {
  532. pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE));
  533. }
  534. /**
  535. * Implements hook_taxonomy_term_delete().
  536. */
  537. function pathauto_taxonomy_term_delete($term) {
  538. pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}");
  539. }
  540. /**
  541. * Implements hook_form_FORM_ID_alter().
  542. *
  543. * Add the Pathauto settings to the taxonomy term form.
  544. */
  545. function pathauto_form_taxonomy_form_term_alter(&$form, $form_state) {
  546. $term = $form_state['term'];
  547. $langcode = pathauto_entity_language('taxonomy_term', $term);
  548. pathauto_field_attach_form('taxonomy_term', $term, $form, $form_state, $langcode);
  549. }
  550. /**
  551. * Update the URL aliases for an individual taxonomy term.
  552. *
  553. * @param $term
  554. * A taxonomy term object.
  555. * @param $op
  556. * Operation being performed on the term ('insert', 'update' or 'bulkupdate').
  557. * @param $options
  558. * An optional array of additional options.
  559. */
  560. function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) {
  561. // Skip processing if the user has disabled pathauto for the term.
  562. if (isset($term->path['pathauto']) && empty($term->path['pathauto'])) {
  563. return;
  564. }
  565. $module = 'taxonomy_term';
  566. if ($term->vid == variable_get('forum_nav_vocabulary', '')) {
  567. if (module_exists('forum')) {
  568. $module = 'forum';
  569. }
  570. else {
  571. return;
  572. }
  573. }
  574. // Check that the term has its bundle, which is the vocabulary's machine name.
  575. if (!isset($term->vocabulary_machine_name)) {
  576. $vocabulary = taxonomy_vocabulary_load($term->vid);
  577. $term->vocabulary_machine_name = $vocabulary->machine_name;
  578. }
  579. $options += array(
  580. 'alias children' => FALSE,
  581. 'language' => pathauto_entity_language('taxonomy_term', $term),
  582. );
  583. // Skip processing if the term has no pattern.
  584. if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) {
  585. return;
  586. }
  587. module_load_include('inc', 'pathauto');
  588. $uri = entity_uri('taxonomy_term', $term);
  589. pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']);
  590. if (!empty($options['alias children'])) {
  591. // For all children generate new aliases.
  592. $options['alias children'] = FALSE;
  593. unset($options['language']);
  594. foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) {
  595. pathauto_taxonomy_term_update_alias($subterm, $op, $options);
  596. }
  597. }
  598. }
  599. /**
  600. * Update the URL aliases for multiple taxonomy terms.
  601. *
  602. * @param $tids
  603. * An array of term IDs.
  604. * @param $op
  605. * Operation being performed on the nodes ('insert', 'update' or
  606. * 'bulkupdate').
  607. * @param $options
  608. * An optional array of additional options.
  609. */
  610. function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
  611. $options += array('message' => FALSE);
  612. $terms = taxonomy_term_load_multiple($tids);
  613. foreach ($terms as $term) {
  614. pathauto_taxonomy_term_update_alias($term, $op, $options);
  615. }
  616. if (!empty($options['message'])) {
  617. drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
  618. }
  619. }
  620. /**
  621. * Update action wrapper for pathauto_taxonomy_term_update_alias().
  622. */
  623. function pathauto_taxonomy_term_update_action($term, $context = array()) {
  624. pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array('message' => TRUE));
  625. }
  626. /**
  627. * @} End of "name pathauto_taxonomy".
  628. */
  629. /**
  630. * @name pathauto_user Pathauto integration for the core user and blog modules.
  631. * @{
  632. */
  633. /**
  634. * Implements hook_user_insert().
  635. */
  636. function pathauto_user_insert(&$edit, $account, $category) {
  637. pathauto_user_update_alias($account, 'insert');
  638. }
  639. /**
  640. * Implements hook_user_update().
  641. */
  642. function pathauto_user_update(&$edit, $account, $category) {
  643. pathauto_user_update_alias($account, 'update');
  644. }
  645. /**
  646. * Implements hook_user_delete().
  647. */
  648. function pathauto_user_delete($account) {
  649. pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}");
  650. pathauto_path_delete_all("blog/{$account->uid}");
  651. }
  652. /**
  653. * Implements hook_user_operations().
  654. */
  655. function pathauto_user_operations() {
  656. $operations['pathauto_update_alias'] = array(
  657. 'label' => t('Update URL alias'),
  658. 'callback' => 'pathauto_user_update_alias_multiple',
  659. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  660. );
  661. return $operations;
  662. }
  663. /**
  664. * Update the URL aliases for an individual user account.
  665. *
  666. * @param $account
  667. * A user account object.
  668. * @param $op
  669. * Operation being performed on the account ('insert', 'update' or
  670. * 'bulkupdate').
  671. * @param $options
  672. * An optional array of additional options.
  673. */
  674. function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) {
  675. // Skip processing if the user has disabled pathauto for the account.
  676. if (isset($account->path['pathauto']) && empty($account->path['pathauto'])) {
  677. return;
  678. }
  679. $options += array(
  680. 'alias blog' => module_exists('blog'),
  681. // $user->language is not the user entity language, thus we need to skip
  682. // the property fallback check.
  683. 'language' => pathauto_entity_language('user', $account, FALSE),
  684. );
  685. // Skip processing if the account has no pattern.
  686. if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
  687. return;
  688. }
  689. module_load_include('inc', 'pathauto');
  690. $uri = entity_uri('user', $account);
  691. pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']);
  692. // Because blogs are also associated with users, also generate the blog paths.
  693. if (!empty($options['alias blog'])) {
  694. pathauto_blog_update_alias($account, $op, $options);
  695. }
  696. }
  697. /**
  698. * Update the URL aliases for multiple user accounts.
  699. *
  700. * @param $uids
  701. * An array of user account IDs.
  702. * @param $op
  703. * Operation being performed on the accounts ('insert', 'update' or
  704. * 'bulkupdate').
  705. * @param $options
  706. * An optional array of additional options.
  707. */
  708. function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) {
  709. $options += array('message' => FALSE);
  710. $accounts = user_load_multiple($uids);
  711. foreach ($accounts as $account) {
  712. pathauto_user_update_alias($account, $op, $options);
  713. }
  714. if (!empty($options['message'])) {
  715. drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
  716. }
  717. }
  718. /**
  719. * Update action wrapper for pathauto_user_update_alias().
  720. */
  721. function pathauto_user_update_action($account, $context = array()) {
  722. pathauto_user_update_alias($account, 'bulkupdate', array('message' => TRUE));
  723. }
  724. /**
  725. * Update the blog URL aliases for an individual user account.
  726. *
  727. * @param $account
  728. * A user account object.
  729. * @param $op
  730. * Operation being performed on the blog ('insert', 'update' or
  731. * 'bulkupdate').
  732. * @param $options
  733. * An optional array of additional options.
  734. */
  735. function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) {
  736. // Skip processing if the blog has no pattern.
  737. if (!pathauto_pattern_load_by_entity('blog')) {
  738. return;
  739. }
  740. $options += array(
  741. 'language' => LANGUAGE_NONE,
  742. );
  743. module_load_include('inc', 'pathauto');
  744. if (node_access('create', 'blog', $account)) {
  745. pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']);
  746. }
  747. else {
  748. pathauto_path_delete_all("blog/{$account->uid}");
  749. }
  750. }
  751. /**
  752. * @} End of "name pathauto_user".
  753. */

Related topics