pathauto.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. );
  346. $info['pathauto_taxonomy_term_update_action'] = array(
  347. 'type' => 'taxonomy_term',
  348. 'label' => t('Update taxonomy term alias'),
  349. 'configurable' => FALSE,
  350. );
  351. $info['pathauto_user_update_action'] = array(
  352. 'type' => 'user',
  353. 'label' => t('Update user alias'),
  354. 'configurable' => FALSE,
  355. );
  356. return $info;
  357. }
  358. /**
  359. * Returns the language code of the given entity.
  360. *
  361. * Backward compatibility layer to ensure that installations running an older
  362. * version of core where entity_language() is not avilable do not break.
  363. *
  364. * @param string $entity_type
  365. * An entity type.
  366. * @param object $entity
  367. * An entity object.
  368. * @param bool $check_language_property
  369. * A boolean if TRUE, will attempt to fetch the language code from
  370. * $entity->language if the entity_language() function failed or does not
  371. * exist. Default is TRUE.
  372. */
  373. function pathauto_entity_language($entity_type, $entity, $check_language_property = TRUE) {
  374. $langcode = NULL;
  375. if (function_exists('entity_language')) {
  376. $langcode = entity_language($entity_type, $entity);
  377. }
  378. elseif ($check_language_property && !empty($entity->language)) {
  379. $langcode = $entity->language;
  380. }
  381. return !empty($langcode) ? $langcode : LANGUAGE_NONE;
  382. }
  383. if (!function_exists('path_field_extra_fields')) {
  384. /**
  385. * Implements hook_field_extra_fields() on behalf of path.module.
  386. *
  387. * Add support for the 'URL path settings' to be re-ordered by the user on the
  388. * 'Manage Fields' tab of content types and vocabularies.
  389. */
  390. function path_field_extra_fields() {
  391. $info = array();
  392. foreach (node_type_get_types() as $node_type) {
  393. if (!isset($info['node'][$node_type->type]['form']['path'])) {
  394. $info['node'][$node_type->type]['form']['path'] = array(
  395. 'label' => t('URL path settings'),
  396. 'description' => t('Path module form elements'),
  397. 'weight' => 30,
  398. );
  399. }
  400. }
  401. if (module_exists('taxonomy')) {
  402. $vocabularies = taxonomy_get_vocabularies();
  403. foreach ($vocabularies as $vocabulary) {
  404. if (!isset($info['taxonomy_term'][$vocabulary->machine_name]['form']['path'])) {
  405. $info['taxonomy_term'][$vocabulary->machine_name]['form']['path'] = array(
  406. 'label' => t('URL path settings'),
  407. 'description' => t('Path module form elements'),
  408. 'weight' => 30,
  409. );
  410. }
  411. }
  412. }
  413. return $info;
  414. }
  415. }
  416. /**
  417. * @name pathauto_node Pathauto integration for the core node module.
  418. * @{
  419. */
  420. /**
  421. * Implements hook_node_insert().
  422. */
  423. function pathauto_node_insert($node) {
  424. // @todo Remove the next line when http://drupal.org/node/1025870 is fixed.
  425. unset($node->uri);
  426. pathauto_node_update_alias($node, 'insert');
  427. }
  428. /**
  429. * Implements hook_node_update().
  430. */
  431. function pathauto_node_update($node) {
  432. pathauto_node_update_alias($node, 'update');
  433. }
  434. /**
  435. * Implements hook_node_delete().
  436. */
  437. function pathauto_node_delete($node) {
  438. pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}");
  439. }
  440. /**
  441. * Implements hook_form_BASE_FORM_ID_alter().
  442. *
  443. * Add the Pathauto settings to the node form.
  444. */
  445. function pathauto_form_node_form_alter(&$form, &$form_state) {
  446. $node = $form_state['node'];
  447. $langcode = pathauto_entity_language('node', $node);
  448. pathauto_field_attach_form('node', $node, $form, $form_state, $langcode);
  449. }
  450. /**
  451. * Implements hook_node_operations().
  452. */
  453. function pathauto_node_operations() {
  454. $operations['pathauto_update_alias'] = array(
  455. 'label' => t('Update URL alias'),
  456. 'callback' => 'pathauto_node_update_alias_multiple',
  457. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  458. );
  459. return $operations;
  460. }
  461. /**
  462. * Update the URL aliases for an individual node.
  463. *
  464. * @param $node
  465. * A node object.
  466. * @param $op
  467. * Operation being performed on the node ('insert', 'update' or 'bulkupdate').
  468. * @param $options
  469. * An optional array of additional options.
  470. */
  471. function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) {
  472. // Skip processing if the user has disabled pathauto for the node.
  473. if (isset($node->path['pathauto']) && empty($node->path['pathauto'])) {
  474. return;
  475. }
  476. $options += array('language' => pathauto_entity_language('node', $node));
  477. // Skip processing if the node has no pattern.
  478. if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
  479. return;
  480. }
  481. module_load_include('inc', 'pathauto');
  482. $uri = entity_uri('node', $node);
  483. pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']);
  484. }
  485. /**
  486. * Update the URL aliases for multiple nodes.
  487. *
  488. * @param $nids
  489. * An array of node IDs.
  490. * @param $op
  491. * Operation being performed on the nodes ('insert', 'update' or
  492. * 'bulkupdate').
  493. * @param $options
  494. * An optional array of additional options.
  495. */
  496. function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) {
  497. $options += array('message' => FALSE);
  498. $nodes = node_load_multiple($nids);
  499. foreach ($nodes as $node) {
  500. pathauto_node_update_alias($node, $op, $options);
  501. }
  502. if (!empty($options['message'])) {
  503. drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
  504. }
  505. }
  506. /**
  507. * Update action wrapper for pathauto_node_update_alias().
  508. */
  509. function pathauto_node_update_action($node, $context = array()) {
  510. pathauto_node_update_alias($node, 'bulkupdate', array('message' => TRUE));
  511. }
  512. /**
  513. * @} End of "name pathauto_node".
  514. */
  515. /**
  516. * @name pathauto_taxonomy Pathauto integration for the core taxonomy module.
  517. * @{
  518. */
  519. /**
  520. * Implements hook_taxonomy_term_insert().
  521. */
  522. function pathauto_taxonomy_term_insert($term) {
  523. pathauto_taxonomy_term_update_alias($term, 'insert');
  524. }
  525. /**
  526. * Implements hook_taxonomy_term_update().
  527. */
  528. function pathauto_taxonomy_term_update($term) {
  529. pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE));
  530. }
  531. /**
  532. * Implements hook_taxonomy_term_delete().
  533. */
  534. function pathauto_taxonomy_term_delete($term) {
  535. pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}");
  536. }
  537. /**
  538. * Implements hook_form_FORM_ID_alter().
  539. *
  540. * Add the Pathauto settings to the taxonomy term form.
  541. */
  542. function pathauto_form_taxonomy_form_term_alter(&$form, $form_state) {
  543. $term = $form_state['term'];
  544. $langcode = pathauto_entity_language('taxonomy_term', $term);
  545. pathauto_field_attach_form('taxonomy_term', $term, $form, $form_state, $langcode);
  546. }
  547. /**
  548. * Update the URL aliases for an individual taxonomy term.
  549. *
  550. * @param $term
  551. * A taxonomy term object.
  552. * @param $op
  553. * Operation being performed on the term ('insert', 'update' or 'bulkupdate').
  554. * @param $options
  555. * An optional array of additional options.
  556. */
  557. function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) {
  558. // Skip processing if the user has disabled pathauto for the term.
  559. if (isset($term->path['pathauto']) && empty($term->path['pathauto'])) {
  560. return;
  561. }
  562. $module = 'taxonomy_term';
  563. if ($term->vid == variable_get('forum_nav_vocabulary', '')) {
  564. if (module_exists('forum')) {
  565. $module = 'forum';
  566. }
  567. else {
  568. return;
  569. }
  570. }
  571. // Check that the term has its bundle, which is the vocabulary's machine name.
  572. if (!isset($term->vocabulary_machine_name)) {
  573. $vocabulary = taxonomy_vocabulary_load($term->vid);
  574. $term->vocabulary_machine_name = $vocabulary->machine_name;
  575. }
  576. $options += array(
  577. 'alias children' => FALSE,
  578. 'language' => pathauto_entity_language('taxonomy_term', $term),
  579. );
  580. // Skip processing if the term has no pattern.
  581. if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) {
  582. return;
  583. }
  584. module_load_include('inc', 'pathauto');
  585. $uri = entity_uri('taxonomy_term', $term);
  586. pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']);
  587. if (!empty($options['alias children'])) {
  588. // For all children generate new aliases.
  589. $options['alias children'] = FALSE;
  590. unset($options['language']);
  591. foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) {
  592. pathauto_taxonomy_term_update_alias($subterm, $op, $options);
  593. }
  594. }
  595. }
  596. /**
  597. * Update the URL aliases for multiple taxonomy terms.
  598. *
  599. * @param $tids
  600. * An array of term IDs.
  601. * @param $op
  602. * Operation being performed on the nodes ('insert', 'update' or
  603. * 'bulkupdate').
  604. * @param $options
  605. * An optional array of additional options.
  606. */
  607. function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
  608. $options += array('message' => FALSE);
  609. $terms = taxonomy_term_load_multiple($tids);
  610. foreach ($terms as $term) {
  611. pathauto_taxonomy_term_update_alias($term, $op, $options);
  612. }
  613. if (!empty($options['message'])) {
  614. drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
  615. }
  616. }
  617. /**
  618. * Update action wrapper for pathauto_taxonomy_term_update_alias().
  619. */
  620. function pathauto_taxonomy_term_update_action($term, $context = array()) {
  621. pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array('message' => TRUE));
  622. }
  623. /**
  624. * @} End of "name pathauto_taxonomy".
  625. */
  626. /**
  627. * @name pathauto_user Pathauto integration for the core user and blog modules.
  628. * @{
  629. */
  630. /**
  631. * Implements hook_user_insert().
  632. */
  633. function pathauto_user_insert(&$edit, $account, $category) {
  634. pathauto_user_update_alias($account, 'insert');
  635. }
  636. /**
  637. * Implements hook_user_update().
  638. */
  639. function pathauto_user_update(&$edit, $account, $category) {
  640. pathauto_user_update_alias($account, 'update');
  641. }
  642. /**
  643. * Implements hook_user_delete().
  644. */
  645. function pathauto_user_delete($account) {
  646. pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}");
  647. pathauto_path_delete_all("blog/{$account->uid}");
  648. }
  649. /**
  650. * Implements hook_user_operations().
  651. */
  652. function pathauto_user_operations() {
  653. $operations['pathauto_update_alias'] = array(
  654. 'label' => t('Update URL alias'),
  655. 'callback' => 'pathauto_user_update_alias_multiple',
  656. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  657. );
  658. return $operations;
  659. }
  660. /**
  661. * Update the URL aliases for an individual user account.
  662. *
  663. * @param $account
  664. * A user account object.
  665. * @param $op
  666. * Operation being performed on the account ('insert', 'update' or
  667. * 'bulkupdate').
  668. * @param $options
  669. * An optional array of additional options.
  670. */
  671. function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) {
  672. // Skip processing if the user has disabled pathauto for the account.
  673. if (isset($account->path['pathauto']) && empty($account->path['pathauto'])) {
  674. return;
  675. }
  676. $options += array(
  677. 'alias blog' => module_exists('blog'),
  678. // $user->language is not the user entity language, thus we need to skip
  679. // the property fallback check.
  680. 'language' => pathauto_entity_language('user', $account, FALSE),
  681. );
  682. // Skip processing if the account has no pattern.
  683. if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
  684. return;
  685. }
  686. module_load_include('inc', 'pathauto');
  687. $uri = entity_uri('user', $account);
  688. pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']);
  689. // Because blogs are also associated with users, also generate the blog paths.
  690. if (!empty($options['alias blog'])) {
  691. pathauto_blog_update_alias($account, $op, $options);
  692. }
  693. }
  694. /**
  695. * Update the URL aliases for multiple user accounts.
  696. *
  697. * @param $uids
  698. * An array of user account IDs.
  699. * @param $op
  700. * Operation being performed on the accounts ('insert', 'update' or
  701. * 'bulkupdate').
  702. * @param $options
  703. * An optional array of additional options.
  704. */
  705. function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) {
  706. $options += array('message' => FALSE);
  707. $accounts = user_load_multiple($uids);
  708. foreach ($accounts as $account) {
  709. pathauto_user_update_alias($account, $op, $options);
  710. }
  711. if (!empty($options['message'])) {
  712. drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
  713. }
  714. }
  715. /**
  716. * Update action wrapper for pathauto_user_update_alias().
  717. */
  718. function pathauto_user_update_action($account, $context = array()) {
  719. pathauto_user_update_alias($account, 'bulkupdate', array('message' => TRUE));
  720. }
  721. /**
  722. * Update the blog URL aliases for an individual user account.
  723. *
  724. * @param $account
  725. * A user account object.
  726. * @param $op
  727. * Operation being performed on the blog ('insert', 'update' or
  728. * 'bulkupdate').
  729. * @param $options
  730. * An optional array of additional options.
  731. */
  732. function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) {
  733. // Skip processing if the blog has no pattern.
  734. if (!pathauto_pattern_load_by_entity('blog')) {
  735. return;
  736. }
  737. $options += array(
  738. 'language' => LANGUAGE_NONE,
  739. );
  740. module_load_include('inc', 'pathauto');
  741. if (node_access('create', 'blog', $account)) {
  742. pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']);
  743. }
  744. else {
  745. pathauto_path_delete_all("blog/{$account->uid}");
  746. }
  747. }
  748. /**
  749. * @} End of "name pathauto_user".
  750. */