pathauto.module 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. $hooks = array(
  26. 'pathauto',
  27. 'path_alias_types',
  28. 'pathauto_pattern_alter',
  29. 'pathauto_alias_alter',
  30. 'pathauto_is_alias_reserved',
  31. );
  32. return array_fill_keys($hooks, array('group' => 'pathauto'));
  33. }
  34. /**
  35. * Implements hook_help().
  36. */
  37. function pathauto_help($path, $arg) {
  38. switch ($path) {
  39. case 'admin/help#pathauto':
  40. module_load_include('inc', 'pathauto');
  41. $output = '<h3>' . t('About') . '</h3>';
  42. $output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
  43. $output .= '<h3>' . t('Settings') . '</h3>';
  44. $output .= '<dl>';
  45. $output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
  46. $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>';
  47. $output .= '</dl>';
  48. return $output;
  49. case 'admin/config/search/path/update_bulk':
  50. $output = '<p>' . t('Bulk generation will only generate URL aliases for items that currently have no aliases. This is typically used when installing Pathauto on a site that has existing un-aliased content that needs to be aliased in bulk.') . '</p>';
  51. return $output;
  52. }
  53. }
  54. /**
  55. * Implements hook_permission().
  56. */
  57. function pathauto_permission() {
  58. return array(
  59. 'administer pathauto' => array(
  60. 'title' => t('Administer pathauto'),
  61. 'description' => t('Allows a user to configure patterns for automated aliases and bulk delete URL-aliases.'),
  62. ),
  63. 'notify of path changes' => array(
  64. 'title' => t('Notify of Path Changes'),
  65. 'description' => t('Determines whether or not users are notified.'),
  66. ),
  67. );
  68. }
  69. /**
  70. * Implements hook_menu().
  71. */
  72. function pathauto_menu() {
  73. $items['admin/config/search/path/patterns'] = array(
  74. 'title' => 'Patterns',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('pathauto_patterns_form'),
  77. 'access arguments' => array('administer pathauto'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'weight' => 10,
  80. 'file' => 'pathauto.admin.inc',
  81. );
  82. $items['admin/config/search/path/settings'] = array(
  83. 'title' => 'Settings',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('pathauto_settings_form'),
  86. 'access arguments' => array('administer pathauto'),
  87. 'type' => MENU_LOCAL_TASK,
  88. 'weight' => 20,
  89. 'file' => 'pathauto.admin.inc',
  90. );
  91. $items['admin/config/search/path/update_bulk'] = array(
  92. 'title' => 'Bulk generate',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('pathauto_bulk_update_form'),
  95. 'access arguments' => array('administer url aliases'),
  96. 'type' => MENU_LOCAL_TASK,
  97. 'weight' => 30,
  98. 'file' => 'pathauto.admin.inc',
  99. );
  100. $items['admin/config/search/path/delete_bulk'] = array(
  101. 'title' => 'Delete aliases',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('pathauto_admin_delete'),
  104. 'access arguments' => array('administer url aliases'),
  105. 'type' => MENU_LOCAL_TASK,
  106. 'weight' => 40,
  107. 'file' => 'pathauto.admin.inc',
  108. );
  109. return $items;
  110. }
  111. /**
  112. * Load an URL alias pattern by entity, bundle, and language.
  113. *
  114. * @param $entity
  115. * An entity (e.g. node, taxonomy, user, etc.)
  116. * @param $bundle
  117. * A bundle (e.g. content type, vocabulary ID, etc.)
  118. * @param $language
  119. * A language code, defaults to the LANGUAGE_NONE constant.
  120. */
  121. function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = LANGUAGE_NONE) {
  122. $patterns = &drupal_static(__FUNCTION__, array());
  123. $pattern_id = "$entity:$bundle:$language";
  124. if (!isset($patterns[$pattern_id])) {
  125. $variables = array();
  126. if ($language != LANGUAGE_NONE) {
  127. $variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern";
  128. }
  129. if ($bundle) {
  130. $variables[] = "pathauto_{$entity}_{$bundle}_pattern";
  131. }
  132. $variables[] = "pathauto_{$entity}_pattern";
  133. foreach ($variables as $variable) {
  134. if ($pattern = trim(variable_get($variable, ''))) {
  135. break;
  136. }
  137. }
  138. $patterns[$pattern_id] = $pattern;
  139. }
  140. return $patterns[$pattern_id];
  141. }
  142. /**
  143. * Delete multiple URL aliases.
  144. *
  145. * Intent of this is to abstract a potential path_delete_multiple() function
  146. * for Drupal 7 or 8.
  147. *
  148. * @param $pids
  149. * An array of path IDs to delete.
  150. */
  151. function pathauto_path_delete_multiple($pids) {
  152. foreach ($pids as $pid) {
  153. path_delete(array('pid' => $pid));
  154. }
  155. }
  156. /**
  157. * Delete an URL alias and any of its sub-paths.
  158. *
  159. * Given a source like 'node/1' this function will delete any alias that have
  160. * that specific source or any sources that match 'node/1/%'.
  161. *
  162. * @param $source
  163. * An string with a source URL path.
  164. */
  165. function pathauto_path_delete_all($source) {
  166. $sql = "SELECT pid FROM {url_alias} WHERE source = :source OR source LIKE :source_wildcard";
  167. $pids = db_query($sql, array(':source' => $source, ':source_wildcard' => $source . '/%'))->fetchCol();
  168. if ($pids) {
  169. pathauto_path_delete_multiple($pids);
  170. }
  171. }
  172. /**
  173. * Delete an entity URL alias and any of its sub-paths.
  174. *
  175. * This function also checks to see if the default entity URI is different from
  176. * the current entity URI and will delete any of the default aliases.
  177. *
  178. * @param $entity_type
  179. * A string with the entity type.
  180. * @param $entity
  181. * An entity object.
  182. * @param $default_uri
  183. * The optional default uri path for the entity.
  184. */
  185. function pathauto_entity_path_delete_all($entity_type, $entity, $default_uri = NULL) {
  186. $uri = entity_uri($entity_type, $entity);
  187. pathauto_path_delete_all($uri['path']);
  188. if (isset($default_uri) && $uri['path'] != $default_uri) {
  189. pathauto_path_delete_all($default_uri);
  190. }
  191. }
  192. /**
  193. * Implements hook_field_attach_rename_bundle().
  194. *
  195. * Respond to machine name changes for pattern variables.
  196. */
  197. function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  198. $variables = db_select('variable', 'v')
  199. ->fields('v', array('name'))
  200. ->condition('name', db_like("pathauto_{$entity_type}_{$bundle_old}_") . '%', 'LIKE')
  201. ->execute()
  202. ->fetchCol();
  203. foreach ($variables as $variable) {
  204. $value = variable_get($variable, '');
  205. variable_del($variable);
  206. $variable = strtr($variable, array("{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}"));
  207. variable_set($variable, $value);
  208. }
  209. }
  210. /**
  211. * Implements hook_field_attach_delete_bundle().
  212. *
  213. * Respond to sub-types being deleted, their patterns can be removed.
  214. */
  215. function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
  216. $variables = db_select('variable', 'v')
  217. ->fields('v', array('name'))
  218. ->condition('name', db_like("pathauto_{$entity_type}_{$bundle}_") . '%', 'LIKE')
  219. ->execute()
  220. ->fetchCol();
  221. foreach ($variables as $variable) {
  222. variable_del($variable);
  223. }
  224. }
  225. /**
  226. * Implements hook_field_attach_form().
  227. *
  228. * Add the automatic alias form elements to an existing path form fieldset.
  229. */
  230. function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  231. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  232. if (!isset($form['path'])) {
  233. // This entity must be supported by core's path.module first.
  234. // @todo Investigate removing this and supporting all fieldable entities.
  235. return;
  236. }
  237. else {
  238. // Taxonomy terms do not have an actual fieldset for path settings.
  239. // Merge in the defaults.
  240. $form['path'] += array(
  241. '#type' => 'fieldset',
  242. '#title' => t('URL path settings'),
  243. '#collapsible' => TRUE,
  244. '#collapsed' => empty($form['path']['alias']),
  245. '#group' => 'additional_settings',
  246. '#attributes' => array(
  247. 'class' => array('path-form'),
  248. ),
  249. '#access' => user_access('create url aliases') || user_access('administer url aliases'),
  250. '#weight' => 30,
  251. '#tree' => TRUE,
  252. '#element_validate' => array('path_form_element_validate'),
  253. );
  254. }
  255. $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $langcode);
  256. if (empty($pattern)) {
  257. return;
  258. }
  259. if (!isset($entity->path['pathauto'])) {
  260. if (!empty($id)) {
  261. module_load_include('inc', 'pathauto');
  262. $uri = entity_uri($entity_type, $entity);
  263. $pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array($entity_type => $entity), $bundle, $langcode);
  264. if ($pathauto_alias === FALSE) {
  265. // If Pathauto is not going to be able to generate an alias, then we
  266. // should not bother to show the checkbox since it wouldn't do anything.
  267. // Note that if a pattern does apply, but all the tokens currently
  268. // evaluate to empty strings, then $pathauto_alias would equal null and
  269. // not false.
  270. return;
  271. }
  272. else {
  273. $path = drupal_get_path_alias($uri['path'], $langcode);
  274. $entity->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);
  275. }
  276. }
  277. else {
  278. $entity->path['pathauto'] = TRUE;
  279. }
  280. }
  281. // Add JavaScript that will disable the path textfield when the automatic
  282. // alias checkbox is checked.
  283. $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]'] = array('checked' => TRUE);
  284. // Override path.module's vertical tabs summary.
  285. $form['path']['#attached']['js'] = array(
  286. 'vertical-tabs' => drupal_get_path('module', 'pathauto') . '/pathauto.js'
  287. );
  288. $form['path']['pathauto'] = array(
  289. '#type' => 'checkbox',
  290. '#title' => t('Generate automatic URL alias'),
  291. '#default_value' => $entity->path['pathauto'],
  292. '#description' => t('Uncheck this to create a custom alias below.'),
  293. '#weight' => -1,
  294. );
  295. // Add a shortcut link to configure URL alias patterns.
  296. if (drupal_valid_path('admin/config/search/path/patterns')) {
  297. $form['path']['pathauto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/search/path/patterns');
  298. }
  299. if ($entity->path['pathauto'] && !empty($entity->old_alias) && empty($entity->path['alias'])) {
  300. $form['path']['alias']['#default_value'] = $entity->old_alias;
  301. $entity->path['alias'] = $entity->old_alias;
  302. }
  303. // For Pathauto to remember the old alias and prevent the Path module from
  304. // deleting it when Pathauto wants to preserve it.
  305. if (!empty($entity->path['alias'])) {
  306. $form['path']['old_alias'] = array(
  307. '#type' => 'value',
  308. '#value' => $entity->path['alias'],
  309. );
  310. }
  311. }
  312. /**
  313. * Implements hook_entity_load().
  314. */
  315. function pathauto_entity_load($entities, $entity_type) {
  316. // Statically cache which entity types have data in the pathauto_state
  317. // table to avoid unnecessary queries for entities that would not have any
  318. // data anyway.
  319. static $loadable_types;
  320. if (!isset($loadable_types)) {
  321. $loadable_types = &drupal_static(__FUNCTION__);
  322. if (!isset($loadable_types)) {
  323. // Prevent errors if pathauto_update_7006() has not yet been run.
  324. if (!db_table_exists('pathauto_state')) {
  325. $loadable_types = array();
  326. }
  327. else {
  328. $loadable_types = db_query("SELECT DISTINCT entity_type FROM {pathauto_state}")->fetchCol();
  329. }
  330. }
  331. }
  332. // Check if this entity type has loadable records.
  333. if (!in_array($entity_type, $loadable_types)) {
  334. return;
  335. }
  336. $states = pathauto_entity_state_load_multiple($entity_type, array_keys($entities));
  337. foreach ($states as $id => $state) {
  338. if (!isset($entities[$id]->path)) {
  339. $entities[$id]->path = array();
  340. }
  341. if (is_array($entities[$id]->path) && !isset($entities[$id]->path['pathauto'])) {
  342. $entities[$id]->path['pathauto'] = $state;
  343. }
  344. }
  345. }
  346. /**
  347. * Implements hook_entity_presave().
  348. */
  349. function pathauto_entity_presave($entity, $entity_type) {
  350. if (isset($entity->path['pathauto']) && is_array($entity->path)) {
  351. // We must set an empty alias string for the path to prevent saving an
  352. // alias.
  353. $entity->path += array('alias' => '');
  354. }
  355. // About to be saved (before insert/update)
  356. if (!empty($entity->path['pathauto']) && isset($entity->path['old_alias'])
  357. && $entity->path['alias'] == '' && $entity->path['old_alias'] != '') {
  358. /**
  359. * There was an old alias, but when pathauto_perform_alias was checked
  360. * the javascript disabled the textbox which led to an empty value being
  361. * submitted. Restoring the old path-value here prevents the Path module
  362. * from deleting any old alias before Pathauto gets control.
  363. */
  364. $entity->path['alias'] = $entity->path['old_alias'];
  365. }
  366. // Help prevent errors with progromatically creating entities by defining
  367. // path['alias'] as an empty string.
  368. // @see http://drupal.org/node/1328180
  369. // @see http://drupal.org/node/1576552
  370. if (isset($entity->path['pathauto']) && !isset($entity->path['alias'])) {
  371. $entity->path['alias'] = '';
  372. }
  373. }
  374. /**
  375. * Implements hook_entity_insert().
  376. */
  377. function pathauto_entity_insert($entity, $entity_type) {
  378. if (isset($entity->path['pathauto'])) {
  379. pathauto_entity_state_save($entity_type, $entity, $entity->path['pathauto']);
  380. }
  381. }
  382. /**
  383. * Implements hook_entity_update().
  384. */
  385. function pathauto_entity_update($entity, $entity_type) {
  386. if (isset($entity->path['pathauto'])) {
  387. pathauto_entity_state_save($entity_type, $entity, $entity->path['pathauto']);
  388. }
  389. }
  390. /**
  391. * Implements hook_entity_delete().
  392. */
  393. function pathauto_entity_delete($entity, $entity_type) {
  394. if (isset($entity->path['pathauto'])) {
  395. pathauto_entity_state_delete($entity_type, $entity);
  396. }
  397. }
  398. /**
  399. * Load a pathauto state for an entity.
  400. *
  401. * @param string $entity_type
  402. * An entity type.
  403. * @param int $entity_id
  404. * An entity ID.
  405. *
  406. * @return bool
  407. * A value that evaluates to TRUE if Pathauto should control this entity's
  408. * path. A value that evaluates to FALSE if Pathauto should not manage the
  409. * entity's path.
  410. */
  411. function pathauto_entity_state_load($entity_type, $entity_id) {
  412. $pathauto_state = pathauto_entity_state_load_multiple($entity_type, array($entity_id));
  413. return !empty($pathauto_state) ? reset($pathauto_state) : FALSE;
  414. }
  415. /**
  416. * Load a pathauto state for multiple entities.
  417. *
  418. * @param string $entity_type
  419. * The entity type.
  420. * @param int[] $entity_ids
  421. * The array of entity IDs.
  422. *
  423. * @return bool[]
  424. * An array of Pathauto states keyed by entity ID.
  425. */
  426. function pathauto_entity_state_load_multiple($entity_type, $entity_ids) {
  427. return db_query("SELECT entity_id, pathauto FROM {pathauto_state} WHERE entity_type = :entity_type AND entity_id IN (:entity_ids)", array(':entity_type' => $entity_type, ':entity_ids' => $entity_ids))->fetchAllKeyed();
  428. }
  429. /**
  430. * Save the pathauto state for an entity.
  431. *
  432. * @param string $entity_type
  433. * The entity type.
  434. * @param object $entity
  435. * The entity object.
  436. * @param bool $pathauto_state
  437. * A value that evaluates to TRUE means that Pathauto should keep controlling
  438. * this entity's path in the future. A value that evaluates to FALSE means
  439. * that Pathauto should not manage the entity's path.
  440. */
  441. function pathauto_entity_state_save($entity_type, $entity, $pathauto_state) {
  442. list($entity_id) = entity_extract_ids($entity_type, $entity);
  443. db_merge('pathauto_state')
  444. ->key(array(
  445. 'entity_type' => $entity_type,
  446. 'entity_id' => $entity_id,
  447. ))
  448. ->fields(array(
  449. 'pathauto' => $pathauto_state ? 1 : 0,
  450. ))
  451. ->execute();
  452. drupal_static_reset('pathauto_entity_load');
  453. }
  454. /**
  455. * Delete the pathauto state for an entity.
  456. *
  457. * @param string $entity_type
  458. * The entity type.
  459. * @param object $entity
  460. * The entity object.
  461. */
  462. function pathauto_entity_state_delete($entity_type, $entity) {
  463. list($entity_id) = entity_extract_ids($entity_type, $entity);
  464. db_delete('pathauto_state')
  465. ->condition('entity_type', $entity_type)
  466. ->condition('entity_id', $entity_id)
  467. ->execute();
  468. drupal_static_reset('pathauto_entity_load');
  469. }
  470. /**
  471. * Implements hook_action_info().
  472. */
  473. function pathauto_action_info() {
  474. $info['pathauto_node_update_action'] = array(
  475. 'type' => 'node',
  476. 'label' => t('Update node alias'),
  477. 'configurable' => FALSE,
  478. 'triggers' => array(),
  479. );
  480. $info['pathauto_taxonomy_term_update_action'] = array(
  481. 'type' => 'taxonomy_term',
  482. 'label' => t('Update taxonomy term alias'),
  483. 'configurable' => FALSE,
  484. 'triggers' => array(),
  485. );
  486. $info['pathauto_user_update_action'] = array(
  487. 'type' => 'user',
  488. 'label' => t('Update user alias'),
  489. 'configurable' => FALSE,
  490. 'triggers' => array(),
  491. );
  492. return $info;
  493. }
  494. /**
  495. * Returns the language code of the given entity.
  496. *
  497. * Backward compatibility layer to ensure that installations running an older
  498. * version of core where entity_language() is not available do not break.
  499. *
  500. * @param string $entity_type
  501. * An entity type.
  502. * @param object $entity
  503. * An entity object.
  504. * @param bool $check_language_property
  505. * A boolean if TRUE, will attempt to fetch the language code from
  506. * $entity->language if the entity_language() function failed or does not
  507. * exist. Default is TRUE.
  508. */
  509. function pathauto_entity_language($entity_type, $entity, $check_language_property = TRUE) {
  510. $langcode = NULL;
  511. if (function_exists('entity_language')) {
  512. $langcode = entity_language($entity_type, $entity);
  513. }
  514. elseif ($check_language_property && !empty($entity->language)) {
  515. $langcode = $entity->language;
  516. }
  517. return !empty($langcode) ? $langcode : LANGUAGE_NONE;
  518. }
  519. function pathauto_is_alias_reserved($alias, $source, $langcode = LANGUAGE_NONE) {
  520. foreach (module_implements('pathauto_is_alias_reserved') as $module) {
  521. $result = module_invoke($module, 'pathauto_is_alias_reserved', $alias, $source, $langcode);
  522. if (!empty($result)) {
  523. // As soon as the first module says that an alias is in fact reserved,
  524. // then there is no point in checking the rest of the modules.
  525. return TRUE;
  526. }
  527. }
  528. return FALSE;
  529. }
  530. /**
  531. * Implements hook_pathauto_is_alias_reserved() on behalf of path.module.
  532. */
  533. function path_pathauto_is_alias_reserved($alias, $source, $langcode) {
  534. // For language neutral content, we need to make sure the alias doesn't
  535. // collide with any existing aliases. For localized content, just make sure
  536. // it doesn't collide with same language or language neutral aliases.
  537. $query = db_select('url_alias', 'ua')
  538. ->fields('ua', array('pid'))
  539. ->condition('source', $source, '<>')
  540. ->condition('alias', $alias);
  541. if ($langcode != LANGUAGE_NONE) {
  542. $query->condition('language', array($langcode, LANGUAGE_NONE), 'IN');
  543. }
  544. return $query->execute()->rowCount() > 0;
  545. }
  546. /**
  547. * Implements hook_pathauto_is_alias_reserved().
  548. */
  549. function pathauto_pathauto_is_alias_reserved($alias, $source, $langcode) {
  550. module_load_include('inc', 'pathauto');
  551. return _pathauto_path_is_callback($alias);
  552. }
  553. if (!function_exists('path_field_extra_fields')) {
  554. /**
  555. * Implements hook_field_extra_fields() on behalf of path.module.
  556. *
  557. * Add support for the 'URL path settings' to be re-ordered by the user on the
  558. * 'Manage Fields' tab of content types and vocabularies.
  559. */
  560. function path_field_extra_fields() {
  561. $info = array();
  562. foreach (node_type_get_types() as $node_type) {
  563. if (!isset($info['node'][$node_type->type]['form']['path'])) {
  564. $info['node'][$node_type->type]['form']['path'] = array(
  565. 'label' => t('URL path settings'),
  566. 'description' => t('Path module form elements'),
  567. 'weight' => 30,
  568. );
  569. }
  570. }
  571. if (module_exists('taxonomy')) {
  572. $vocabularies = taxonomy_get_vocabularies();
  573. foreach ($vocabularies as $vocabulary) {
  574. if (!isset($info['taxonomy_term'][$vocabulary->machine_name]['form']['path'])) {
  575. $info['taxonomy_term'][$vocabulary->machine_name]['form']['path'] = array(
  576. 'label' => t('URL path settings'),
  577. 'description' => t('Path module form elements'),
  578. 'weight' => 30,
  579. );
  580. }
  581. }
  582. }
  583. return $info;
  584. }
  585. }
  586. /**
  587. * @name pathauto_node Pathauto integration for the core node module.
  588. * @{
  589. */
  590. /**
  591. * Implements hook_path_alias_types() on behalf of node module.
  592. */
  593. function node_path_alias_types() {
  594. return array('node/' => t('Content'));
  595. }
  596. /**
  597. * Implements hook_pathauto() on behalf of node module.
  598. */
  599. function node_pathauto($op) {
  600. if ($op == 'settings') {
  601. $settings = array();
  602. $settings['module'] = 'node';
  603. $settings['token_type'] = 'node';
  604. $settings['groupheader'] = t('Content paths');
  605. $settings['patterndescr'] = t('Default path pattern (applies to all content types with blank patterns below)');
  606. $settings['patterndefault'] = 'content/[node:title]';
  607. $settings['batch_update_callback'] = 'node_pathauto_bulk_update_batch_process';
  608. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  609. $languages = array();
  610. if (module_exists('locale')) {
  611. $languages = array(LANGUAGE_NONE => t('language neutral')) + locale_language_list('name');
  612. }
  613. foreach (node_type_get_names() as $node_type => $node_name) {
  614. if (count($languages) && variable_get('language_content_type_' . $node_type, 0)) {
  615. $settings['patternitems'][$node_type] = t('Default path pattern for @node_type (applies to all @node_type content types with blank patterns below)', array('@node_type' => $node_name));
  616. foreach ($languages as $lang_code => $lang_name) {
  617. $settings['patternitems'][$node_type . '_' . $lang_code] = t('Pattern for all @language @node_type paths', array('@node_type' => $node_name, '@language' => $lang_name));
  618. }
  619. }
  620. else {
  621. $settings['patternitems'][$node_type] = t('Pattern for all @node_type paths', array('@node_type' => $node_name));
  622. }
  623. }
  624. return (object) $settings;
  625. }
  626. }
  627. /**
  628. * Implements hook_node_insert().
  629. */
  630. function pathauto_node_insert($node) {
  631. // @todo Remove the next line when http://drupal.org/node/1025870 is fixed.
  632. unset($node->uri);
  633. pathauto_node_update_alias($node, 'insert');
  634. }
  635. /**
  636. * Implements hook_node_update().
  637. */
  638. function pathauto_node_update($node) {
  639. pathauto_node_update_alias($node, 'update');
  640. }
  641. /**
  642. * Implements hook_node_delete().
  643. */
  644. function pathauto_node_delete($node) {
  645. pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}");
  646. }
  647. /**
  648. * Implements hook_form_BASE_FORM_ID_alter().
  649. *
  650. * Add the Pathauto settings to the node form.
  651. */
  652. function pathauto_form_node_form_alter(&$form, &$form_state) {
  653. $node = $form_state['node'];
  654. $langcode = pathauto_entity_language('node', $node);
  655. pathauto_field_attach_form('node', $node, $form, $form_state, $langcode);
  656. }
  657. /**
  658. * Implements hook_node_operations().
  659. */
  660. function pathauto_node_operations() {
  661. $operations['pathauto_update_alias'] = array(
  662. 'label' => t('Update URL alias'),
  663. 'callback' => 'pathauto_node_update_alias_multiple',
  664. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  665. );
  666. return $operations;
  667. }
  668. /**
  669. * Update the URL aliases for an individual node.
  670. *
  671. * @param $node
  672. * A node object.
  673. * @param $op
  674. * Operation being performed on the node ('insert', 'update' or 'bulkupdate').
  675. * @param $options
  676. * An optional array of additional options.
  677. */
  678. function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) {
  679. // Skip processing if the user has disabled pathauto for the node.
  680. if (isset($node->path['pathauto']) && empty($node->path['pathauto']) && empty($options['force'])) {
  681. return FALSE;
  682. }
  683. $options += array('language' => pathauto_entity_language('node', $node));
  684. // Skip processing if the node has no pattern.
  685. if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
  686. return FALSE;
  687. }
  688. module_load_include('inc', 'pathauto');
  689. $uri = entity_uri('node', $node);
  690. return pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']);
  691. }
  692. /**
  693. * Update the URL aliases for multiple nodes.
  694. *
  695. * @param $nids
  696. * An array of node IDs.
  697. * @param $op
  698. * Operation being performed on the nodes ('insert', 'update' or
  699. * 'bulkupdate').
  700. * @param $options
  701. * An optional array of additional options.
  702. */
  703. function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) {
  704. $options += array('message' => FALSE);
  705. $nodes = node_load_multiple($nids);
  706. foreach ($nodes as $node) {
  707. pathauto_node_update_alias($node, $op, $options);
  708. }
  709. if (!empty($options['message'])) {
  710. drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
  711. }
  712. }
  713. /**
  714. * Update action wrapper for pathauto_node_update_alias().
  715. */
  716. function pathauto_node_update_action($node, $context = array()) {
  717. pathauto_node_update_alias($node, 'bulkupdate', array('message' => TRUE));
  718. }
  719. /**
  720. * @} End of "name pathauto_node".
  721. */
  722. /**
  723. * @name pathauto_taxonomy Pathauto integration for the core taxonomy module.
  724. * @{
  725. */
  726. /**
  727. * Implements hook_path_alias_types() on behalf of taxonomy module.
  728. */
  729. function taxonomy_path_alias_types() {
  730. return array('taxonomy/term/' => t('Taxonomy terms'));
  731. }
  732. /**
  733. * Implements hook_pathauto() on behalf of taxonomy module.
  734. */
  735. function taxonomy_pathauto($op) {
  736. if ($op == 'settings') {
  737. $settings = array();
  738. $settings['module'] = 'taxonomy_term';
  739. $settings['token_type'] = 'term';
  740. $settings['groupheader'] = t('Taxonomy term paths');
  741. $settings['patterndescr'] = t('Default path pattern (applies to all vocabularies with blank patterns below)');
  742. $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
  743. $settings['batch_update_callback'] = 'taxonomy_pathauto_bulk_update_batch_process';
  744. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  745. $vocabularies = taxonomy_get_vocabularies();
  746. if (count($vocabularies)) {
  747. $settings['patternitems'] = array();
  748. foreach ($vocabularies as $vid => $vocabulary) {
  749. if ($vid == variable_get('forum_nav_vocabulary', '')) {
  750. // Skip the forum vocabulary.
  751. continue;
  752. }
  753. $settings['patternitems'][$vocabulary->machine_name] = t('Pattern for all %vocab-name paths', array('%vocab-name' => $vocabulary->name));
  754. }
  755. }
  756. return (object) $settings;
  757. }
  758. }
  759. /**
  760. * Implements hook_taxonomy_term_insert().
  761. */
  762. function pathauto_taxonomy_term_insert($term) {
  763. pathauto_taxonomy_term_update_alias($term, 'insert');
  764. }
  765. /**
  766. * Implements hook_taxonomy_term_update().
  767. */
  768. function pathauto_taxonomy_term_update($term) {
  769. pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE));
  770. }
  771. /**
  772. * Implements hook_taxonomy_term_delete().
  773. */
  774. function pathauto_taxonomy_term_delete($term) {
  775. pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}");
  776. }
  777. /**
  778. * Implements hook_form_FORM_ID_alter().
  779. *
  780. * Add the Pathauto settings to the taxonomy term form.
  781. */
  782. function pathauto_form_taxonomy_form_term_alter(&$form, $form_state) {
  783. $term = $form_state['term'];
  784. $langcode = pathauto_entity_language('taxonomy_term', $term);
  785. pathauto_field_attach_form('taxonomy_term', $term, $form, $form_state, $langcode);
  786. }
  787. /**
  788. * Update the URL aliases for an individual taxonomy term.
  789. *
  790. * @param $term
  791. * A taxonomy term object.
  792. * @param $op
  793. * Operation being performed on the term ('insert', 'update' or 'bulkupdate').
  794. * @param $options
  795. * An optional array of additional options.
  796. */
  797. function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) {
  798. // Skip processing if the user has disabled pathauto for the term.
  799. if (isset($term->path['pathauto']) && empty($term->path['pathauto']) && empty($options['force'])) {
  800. return FALSE;
  801. }
  802. $module = 'taxonomy_term';
  803. if ($term->vid == variable_get('forum_nav_vocabulary', '')) {
  804. if (module_exists('forum')) {
  805. $module = 'forum';
  806. }
  807. else {
  808. return FALSE;
  809. }
  810. }
  811. // Check that the term has its bundle, which is the vocabulary's machine name.
  812. if (!isset($term->vocabulary_machine_name)) {
  813. $vocabulary = taxonomy_vocabulary_load($term->vid);
  814. $term->vocabulary_machine_name = $vocabulary->machine_name;
  815. }
  816. $options += array(
  817. 'alias children' => FALSE,
  818. 'language' => pathauto_entity_language('taxonomy_term', $term),
  819. );
  820. // Skip processing if the term has no pattern.
  821. if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) {
  822. return FALSE;
  823. }
  824. module_load_include('inc', 'pathauto');
  825. $uri = entity_uri('taxonomy_term', $term);
  826. $result = pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']);
  827. if (!empty($options['alias children'])) {
  828. // For all children generate new aliases.
  829. unset($options['language']);
  830. foreach (taxonomy_get_children($term->tid, $term->vid) as $subterm) {
  831. pathauto_taxonomy_term_update_alias($subterm, $op, $options);
  832. }
  833. }
  834. return $result;
  835. }
  836. /**
  837. * Update the URL aliases for multiple taxonomy terms.
  838. *
  839. * @param $tids
  840. * An array of term IDs.
  841. * @param $op
  842. * Operation being performed on the nodes ('insert', 'update' or
  843. * 'bulkupdate').
  844. * @param $options
  845. * An optional array of additional options.
  846. */
  847. function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
  848. $options += array('message' => FALSE);
  849. $terms = taxonomy_term_load_multiple($tids);
  850. foreach ($terms as $term) {
  851. pathauto_taxonomy_term_update_alias($term, $op, $options);
  852. }
  853. if (!empty($options['message'])) {
  854. drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
  855. }
  856. }
  857. /**
  858. * Update action wrapper for pathauto_taxonomy_term_update_alias().
  859. */
  860. function pathauto_taxonomy_term_update_action($term, $context = array()) {
  861. pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array('message' => TRUE));
  862. }
  863. /**
  864. * @} End of "name pathauto_taxonomy".
  865. */
  866. /**
  867. * @name pathauto_forum Pathauto integration for the core forum module.
  868. * @{
  869. */
  870. /**
  871. * Implements hook_path_alias_types() on behalf of forum module.
  872. */
  873. function forum_path_alias_types() {
  874. return array('forum/' => t('Forums'));
  875. }
  876. /**
  877. * Implements hook_pathauto() for forum module.
  878. */
  879. function forum_pathauto($op) {
  880. if ($op == 'settings') {
  881. $settings = array();
  882. $settings['module'] = 'forum';
  883. $settings['token_type'] = 'term';
  884. $settings['groupheader'] = t('Forum paths');
  885. $settings['patterndescr'] = t('Pattern for forums and forum containers');
  886. $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
  887. $settings['batch_update_callback'] = 'forum_pathauto_bulk_update_batch_process';
  888. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  889. return (object) $settings;
  890. }
  891. }
  892. /**
  893. * @} End of "name pathauto_forum".
  894. */
  895. /**
  896. * @name pathauto_user Pathauto integration for the core user and blog modules.
  897. * @{
  898. */
  899. /**
  900. * Implements hook_path_alias_types() on behalf of user module.
  901. */
  902. function user_path_alias_types() {
  903. return array('user/' => t('Users'));
  904. }
  905. /**
  906. * Implements hook_pathauto() on behalf of user module.
  907. */
  908. function user_pathauto($op) {
  909. if ($op == 'settings') {
  910. $settings = array();
  911. $settings['module'] = 'user';
  912. $settings['token_type'] = 'user';
  913. $settings['groupheader'] = t('User paths');
  914. $settings['patterndescr'] = t('Pattern for user account page paths');
  915. $settings['patterndefault'] = 'users/[user:name]';
  916. $settings['batch_update_callback'] = 'user_pathauto_bulk_update_batch_process';
  917. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  918. return (object) $settings;
  919. }
  920. }
  921. /**
  922. * Implements hook_user_insert().
  923. */
  924. function pathauto_user_insert(&$edit, $account, $category) {
  925. pathauto_user_update_alias($account, 'insert');
  926. }
  927. /**
  928. * Implements hook_user_update().
  929. */
  930. function pathauto_user_update(&$edit, $account, $category) {
  931. pathauto_user_update_alias($account, 'update');
  932. }
  933. /**
  934. * Implements hook_user_delete().
  935. */
  936. function pathauto_user_delete($account) {
  937. pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}");
  938. pathauto_path_delete_all("blog/{$account->uid}");
  939. }
  940. /**
  941. * Implements hook_user_operations().
  942. */
  943. function pathauto_user_operations() {
  944. $operations['pathauto_update_alias'] = array(
  945. 'label' => t('Update URL alias'),
  946. 'callback' => 'pathauto_user_update_alias_multiple',
  947. 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  948. );
  949. return $operations;
  950. }
  951. /**
  952. * Update the URL aliases for an individual user account.
  953. *
  954. * @param $account
  955. * A user account object.
  956. * @param $op
  957. * Operation being performed on the account ('insert', 'update' or
  958. * 'bulkupdate').
  959. * @param $options
  960. * An optional array of additional options.
  961. */
  962. function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) {
  963. // Skip processing if the user has disabled pathauto for the account.
  964. if (isset($account->path['pathauto']) && empty($account->path['pathauto']) && empty($options['force'])) {
  965. return FALSE;
  966. }
  967. $options += array(
  968. 'alias blog' => module_exists('blog'),
  969. // $user->language is not the user entity language, thus we need to skip
  970. // the property fallback check.
  971. 'language' => pathauto_entity_language('user', $account, FALSE),
  972. );
  973. // Skip processing if the account has no pattern.
  974. if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
  975. return FALSE;
  976. }
  977. module_load_include('inc', 'pathauto');
  978. $uri = entity_uri('user', $account);
  979. $return = pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']);
  980. // Because blogs are also associated with users, also generate the blog paths.
  981. if (!empty($options['alias blog'])) {
  982. pathauto_blog_update_alias($account, $op, $options);
  983. }
  984. return $return;
  985. }
  986. /**
  987. * Update the URL aliases for multiple user accounts.
  988. *
  989. * @param $uids
  990. * An array of user account IDs.
  991. * @param $op
  992. * Operation being performed on the accounts ('insert', 'update' or
  993. * 'bulkupdate').
  994. * @param $options
  995. * An optional array of additional options.
  996. */
  997. function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) {
  998. $options += array('message' => FALSE);
  999. $accounts = user_load_multiple($uids);
  1000. foreach ($accounts as $account) {
  1001. pathauto_user_update_alias($account, $op, $options);
  1002. }
  1003. if (!empty($options['message'])) {
  1004. drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
  1005. }
  1006. }
  1007. /**
  1008. * Update action wrapper for pathauto_user_update_alias().
  1009. */
  1010. function pathauto_user_update_action($account, $context = array()) {
  1011. pathauto_user_update_alias($account, 'bulkupdate', array('message' => TRUE));
  1012. }
  1013. /**
  1014. * @} End of "name pathauto_user".
  1015. */
  1016. /**
  1017. * @name pathauto_blog Pathauto integration for the core blog module.
  1018. * @{
  1019. */
  1020. /**
  1021. * Implements hook_path_alias_types() on behalf of blog module.
  1022. */
  1023. function blog_path_alias_types() {
  1024. return array('blog/' => t('User blogs'));
  1025. }
  1026. /**
  1027. * Implements hook_pathauto() on behalf of blog module.
  1028. */
  1029. function blog_pathauto($op) {
  1030. if ($op == 'settings') {
  1031. $settings = array();
  1032. $settings['module'] = 'blog';
  1033. $settings['token_type'] = 'user';
  1034. $settings['groupheader'] = t('Blog paths');
  1035. $settings['patterndescr'] = t('Pattern for blog page paths');
  1036. $settings['patterndefault'] = 'blogs/[user:name]';
  1037. $settings['batch_update_callback'] = 'blog_pathauto_bulk_update_batch_process';
  1038. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  1039. return (object) $settings;
  1040. }
  1041. }
  1042. /**
  1043. * Update the blog URL aliases for an individual user account.
  1044. *
  1045. * @param $account
  1046. * A user account object.
  1047. * @param $op
  1048. * Operation being performed on the blog ('insert', 'update' or
  1049. * 'bulkupdate').
  1050. * @param $options
  1051. * An optional array of additional options.
  1052. */
  1053. function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) {
  1054. // Skip processing if the blog has no pattern.
  1055. if (!pathauto_pattern_load_by_entity('blog')) {
  1056. return FALSE;
  1057. }
  1058. $options += array(
  1059. 'language' => LANGUAGE_NONE,
  1060. );
  1061. module_load_include('inc', 'pathauto');
  1062. if (node_access('create', 'blog', $account)) {
  1063. return pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']);
  1064. }
  1065. else {
  1066. pathauto_path_delete_all("blog/{$account->uid}");
  1067. }
  1068. }
  1069. /**
  1070. * @} End of "name pathauto_blog".
  1071. */
  1072. /**
  1073. * Implements hook_features_pipe_COMPONENT_alter().
  1074. */
  1075. function pathauto_features_pipe_node_alter(&$pipe, $data, $export) {
  1076. foreach ($data as $node_type) {
  1077. $pipe['variable'][] = "pathauto_node_{$node_type}_pattern";
  1078. if (module_exists('locale')) {
  1079. $langcodes = array_keys(locale_language_list('name'));
  1080. $langcodes[] = LANGUAGE_NONE;
  1081. foreach ($langcodes as $langcode) {
  1082. $pipe['variable'][] = "pathauto_node_{$node_type}_{$langcode}_pattern";
  1083. }
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * Implements hook_features_pipe_COMPONENT_alter().
  1089. */
  1090. function pathauto_features_pipe_taxonomy_alter(&$pipe, $data, $export) {
  1091. foreach ($data as $vocabulary) {
  1092. $pipe['variable'][] = "pathauto_taxonomy_term_{$vocabulary}_pattern";
  1093. }
  1094. }