pathauto.api.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for pathauto API.
  5. *
  6. * It may be helpful to review some examples of integration from
  7. * pathauto.pathauto.inc.
  8. *
  9. * Pathauto works by using tokens in path patterns. Thus the simplest
  10. * integration is just to provide tokens. Token support is provided by Drupal
  11. * core. To provide additional token from your module, implement the following
  12. * hooks:
  13. *
  14. * hook_tokens() - http://api.drupal.org/api/function/hook_tokens
  15. * hook_token_info() - http://api.drupal.org/api/function/hook_token_info
  16. *
  17. * If you wish to provide pathauto integration for custom paths provided by your
  18. * module, there are a few steps involved.
  19. *
  20. * 1. hook_pathauto()
  21. * Provide information required by pathauto for the settings form as well as
  22. * bulk generation. See the documentation for hook_pathauto() for more
  23. * details.
  24. *
  25. * 2. pathauto_create_alias()
  26. * At the appropriate time (usually when a new item is being created for
  27. * which a generated alias is desired), call pathauto_create_alias() with the
  28. * appropriate parameters to generate and create the alias. See the user,
  29. * taxonomy, and node hook implementations in pathauto.module for examples.
  30. * Also see the documentation for pathauto_create_alias().
  31. *
  32. * 3. pathauto_path_delete_all()
  33. * At the appropriate time (usually when an item is being deleted), call
  34. * pathauto_path_delete_all() to remove any aliases that were created for the
  35. * content being removed. See the documentation for
  36. * pathauto_path_delete_all() for more details.
  37. *
  38. * 4. hook_path_alias_types()
  39. * For modules that create new types of content that can be aliased with
  40. * pathauto, a hook implementation is needed to allow the user to delete them
  41. * all at once. See the documentation for hook_path_alias_types() below for
  42. * more information.
  43. *
  44. * There are other integration points with pathauto, namely alter hooks that
  45. * allow you to change the data used by pathauto at various points in the
  46. * process. See the below hook documentation for details.
  47. */
  48. /**
  49. * Used primarily by the bulk delete form. This hooks provides pathauto the
  50. * information needed to bulk delete aliases created by your module. The keys
  51. * of the return array are used by pathauto as the system path prefix to delete
  52. * from the url_aliases table. The corresponding value is simply used as the
  53. * label for each type of path on the bulk delete form.
  54. *
  55. * @return
  56. * An array whose keys match the beginning of the source paths
  57. * (e.g.: "node/", "user/", etc.) and whose values describe the type of page
  58. * (e.g.: "Content", "Users"). Like all displayed strings, these descriptions
  59. * should be localized with t(). Use % to match interior pieces of a path,
  60. * like "user/%/track". This is a database wildcard (meaning "user/%/track"
  61. * matches "user/1/track" as well as "user/1/view/track").
  62. */
  63. function hook_path_alias_types() {
  64. $objects['user/'] = t('Users');
  65. $objects['node/'] = t('Content');
  66. return $objects;
  67. }
  68. /**
  69. * Provide information about the way your module's aliases will be built.
  70. *
  71. * The information you provide here is used to build the form
  72. * on search/path/patterns. File pathauto.pathauto.inc provides example
  73. * implementations for system modules.
  74. *
  75. * @see node_pathauto
  76. *
  77. * @param $op
  78. * At the moment this will always be 'settings'.
  79. *
  80. * @return object|null
  81. * An object, or array of objects (if providing multiple groups of path
  82. * patterns). Each object should have the following members:
  83. * - 'module': The module or entity type.
  84. * - 'token_type': Which token type should be allowed in the patterns form.
  85. * - 'groupheader': Translated label for the settings group
  86. * - 'patterndescr': The translated label for the default pattern (e.g.,
  87. * t('Default path pattern (applies to all content types with blank
  88. * patterns below)')
  89. * - 'patterndefault': Default pattern (e.g. 'content/[node:title]'
  90. * - 'batch_update_callback': The name of function that should be ran for
  91. * bulk update. @see node_pathauto_bulk_update_batch_process for example
  92. * - 'batch_file': The name of the file with the bulk update function.
  93. * - 'patternitems': Optional. An array of descritpions keyed by bundles.
  94. */
  95. function hook_pathauto($op) {
  96. switch ($op) {
  97. case 'settings':
  98. $settings = array();
  99. $settings['module'] = 'file';
  100. $settings['token_type'] = 'file';
  101. $settings['groupheader'] = t('File paths');
  102. $settings['patterndescr'] = t('Default path pattern (applies to all file types with blank patterns below)');
  103. $settings['patterndefault'] = 'files/[file:name]';
  104. $settings['batch_update_callback'] = 'file_entity_pathauto_bulk_update_batch_process';
  105. $settings['batch_file'] = drupal_get_path('module', 'file_entity') . '/file_entity.pathauto.inc';
  106. foreach (file_type_get_enabled_types() as $file_type => $type) {
  107. $settings['patternitems'][$file_type] = t('Pattern for all @file_type paths.', array('@file_type' => $type->label));
  108. }
  109. return (object) $settings;
  110. default:
  111. break;
  112. }
  113. }
  114. /**
  115. * Determine if a possible URL alias would conflict with any existing paths.
  116. *
  117. * Returning TRUE from this function will trigger pathauto_alias_uniquify() to
  118. * generate a similar URL alias with a suffix to avoid conflicts.
  119. *
  120. * @param string $alias
  121. * The potential URL alias.
  122. * @param string $source
  123. * The source path for the alias (e.g. 'node/1').
  124. * @param string $langcode
  125. * The language code for the alias (e.g. 'en').
  126. *
  127. * @return bool
  128. * TRUE if $alias conflicts with an existing, reserved path, or FALSE/NULL if
  129. * it does not match any reserved paths.
  130. *
  131. * @see pathauto_alias_uniquify()
  132. */
  133. function hook_pathauto_is_alias_reserved($alias, $source, $langcode) {
  134. // Check our module's list of paths and return TRUE if $alias matches any of
  135. // them.
  136. return (bool) db_query("SELECT 1 FROM {mytable} WHERE path = :path", array(':path' => $alias))->fetchField();
  137. }
  138. /**
  139. * Alter the pattern to be used before an alias is generated by Pathauto.
  140. *
  141. * This hook will only be called if a default pattern is configured (on
  142. * admin/config/search/path/patterns).
  143. *
  144. * @param string $pattern
  145. * The alias pattern for Pathauto to pass to token_replace() to generate the
  146. * URL alias.
  147. * @param array $context
  148. * An associative array of additional options, with the following elements:
  149. * - 'module': The module or entity type being aliased.
  150. * - 'op': A string with the operation being performed on the object being
  151. * aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
  152. * - 'source': A string of the source path for the alias (e.g. 'node/1').
  153. * - 'data': An array of keyed objects to pass to token_replace().
  154. * - 'type': The sub-type or bundle of the object being aliased.
  155. * - 'language': A string of the language code for the alias (e.g. 'en').
  156. * This can be altered by reference.
  157. */
  158. function hook_pathauto_pattern_alter(&$pattern, array $context) {
  159. // Switch out any [node:created:*] tokens with [node:updated:*] on update.
  160. if ($context['module'] == 'node' && ($context['op'] == 'update')) {
  161. $pattern = preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern);
  162. }
  163. }
  164. /**
  165. * Alter Pathauto-generated aliases before saving.
  166. *
  167. * @param string $alias
  168. * The automatic alias after token replacement and strings cleaned.
  169. * @param array $context
  170. * An associative array of additional options, with the following elements:
  171. * - 'module': The module or entity type being aliased.
  172. * - 'op': A string with the operation being performed on the object being
  173. * aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
  174. * - 'source': A string of the source path for the alias (e.g. 'node/1').
  175. * This can be altered by reference.
  176. * - 'data': An array of keyed objects to pass to token_replace().
  177. * - 'type': The sub-type or bundle of the object being aliased.
  178. * - 'language': A string of the language code for the alias (e.g. 'en').
  179. * This can be altered by reference.
  180. * - 'pattern': A string of the pattern used for aliasing the object.
  181. */
  182. function hook_pathauto_alias_alter(&$alias, array &$context) {
  183. // Add a suffix so that all aliases get saved as 'content/my-title.html'
  184. $alias .= '.html';
  185. // Force all aliases to be saved as language neutral.
  186. $context['language'] = LANGUAGE_NONE;
  187. }
  188. /**
  189. * Alter the list of punctuation characters for Pathauto control.
  190. *
  191. * @param $punctuation
  192. * An array of punctuation to be controlled by Pathauto during replacement
  193. * keyed by punctuation name. Each punctuation record should be an array
  194. * with the following key/value pairs:
  195. * - value: The raw value of the punctuation mark.
  196. * - name: The human-readable name of the punctuation mark. This must be
  197. * translated using t() already.
  198. */
  199. function hook_pathauto_punctuation_chars_alter(array &$punctuation) {
  200. // Add the trademark symbol.
  201. $punctuation['trademark'] = array('value' => '™', 'name' => t('Trademark symbol'));
  202. // Remove the dollar sign.
  203. unset($punctuation['dollar']);
  204. }