You are here

API.txt in Pathauto 5

Same filename and directory in other branches
  1. 5.2 API.txt
  2. 6.2 API.txt
  3. 6 API.txt
Implementing automatic aliases for a module

There are three pieces a module which implements its own content type
must provide to support automatic aliasing (see pathauto_node.inc, 
pathauto_taxonomy.inc, and pathauto_user.inc for examples). Ideally 
this code will be integrated into a module, but a developer may extend 
a module they don't maintain by implementing these functions in a file 
of the form pathauto_<module>.inc in the pathauto directory.

1 - Settings hook

You must implement hook_pathauto($op), where $op is always (at this
time) 'settings'. Return an object (NOT an array) containing the
following members, which will be used by pathauto to build a group
of settings for your module and define the variables for saving your
settings:

module - The name of your module (e.g., 'node')
groupheader - The translated label for the settings group (e.g.,
  t('Node path settings')
patterndescr - The translated label for the default pattern (e.g.,
  t('Default path pattern (applies to all node types with blank patterns below)')
patterndefault - A translated default pattern (e.g., t('[cat]/[title].html'))
placeholders - An array whose keys consist of the translated placeholders
  which will appear in patterns (e.g., t('[title]')) and values are 
  the translated description of the placeholders (e.g.,
  t('The title of the node, with spaces and punctuation.')
patternitems - For modules which need to express multiple patterns
  (for example, the node module supports a separate pattern for each
  node type), an array whose keys consist of identifiers for each
  pattern (e.g., the node type name) and values consist of the
  translated label for the pattern
supportsfeeds - Modules which support RSS feeds should set this to the
  string that's appended to a path for its feed (usually 'feed') , so 
  when administrators enable "Create feed aliases" an alias for this 
  content type's feed will be generated in addition to the base alias.
bulkname - For modules which support a bulk update operation, the
  translated label for the action (e.g., t('Bulk update node paths'))
bulkdescr - For modules which support a bulk update operation, a 
  translated, more thorough description of what the operation will do 
  (e.g., t('Generate aliases for all existing nodes which do not already have aliases.'))

2 - $alias = pathauto_create_alias($module, $op, $placeholders, $src, $type=NULL)

At the appropriate time (usually when a new item is being created for
which a generated alias is desired), call pathauto_create_alias() to
generate and create the alias.

$module - The name of your module (e.g., 'node')
$op - Operation being performed on the item ('insert', 'update', or
  'bulkupdate')
$placeholders - An array whose keys consist of the translated placeholders
  which appear in patterns and values are the "clean" values to be 
  substituted into the pattern. Call pathauto_cleanstring() on any
  values which you do not know to be purely alphanumeric, to substitute
  any non-alphanumerics with the user's designated separator. Note that
  if the pattern has multiple slash-separated components (e.g., [catpath]),
  pathauto_cleanstring() should be called for each component, not the
  complete string.
  Example: $placeholders[t('[title]')] = pathauto_cleanstring($node->title);
$src - The "real" URI of the content to be aliased (e.g., "node/$node->nid")
$type - For modules which provided patternitems in hook_autopath(),
  the relevant identifier for the specific item to be aliased (e.g.,
  $node->type)

pathauto_create_alias() returns the alias that was created.

3 - Bulk update function

If a module supports bulk updating of aliases, it must provide a
function of this form, to be called by pathauto when the corresponding
checkbox is selected and the settings page submitted:

function <module>_pathauto_bulkupdate()

The function should iterate over the content items controlled by the
module, calling pathauto_create_alias() for each one. It is
recommended that the function report on its success (e.g., with a 
count of created aliases) via drupal_set_message().



Modules that extend node and/or taxonomy

Many contributed Drupal modules extend the core node and taxonomy
modules. To extend pathauto patterns to support their extensions, they
may implement the pathauto_node and pathauto_taxonomy hooks.

To do so, implement the function <modulename>_pathauto_node (or _taxonomy),
accepting the arguments $op and $node (or $term). Two operations are
supported:

$op = 'placeholders' - return an array keyed on placeholder strings
(e.g., t('[eventyyyy]')) valued with descriptions (e.g. t('The year the 
event starts.')).
$op = 'values' - return an array keyed on placeholder strings, valued
with the "clean" actual value for the passed node or category (e.g., 
pathauto_cleanstring(date('M', $eventstart)));

See contrib/pathauto_node_event.inc for an example of extending node
patterns.

File

API.txt
View source
  1. Implementing automatic aliases for a module
  2. There are three pieces a module which implements its own content type
  3. must provide to support automatic aliasing (see pathauto_node.inc,
  4. pathauto_taxonomy.inc, and pathauto_user.inc for examples). Ideally
  5. this code will be integrated into a module, but a developer may extend
  6. a module they don't maintain by implementing these functions in a file
  7. of the form pathauto_.inc in the pathauto directory.
  8. 1 - Settings hook
  9. You must implement hook_pathauto($op), where $op is always (at this
  10. time) 'settings'. Return an object (NOT an array) containing the
  11. following members, which will be used by pathauto to build a group
  12. of settings for your module and define the variables for saving your
  13. settings:
  14. module - The name of your module (e.g., 'node')
  15. groupheader - The translated label for the settings group (e.g.,
  16. t('Node path settings')
  17. patterndescr - The translated label for the default pattern (e.g.,
  18. t('Default path pattern (applies to all node types with blank patterns below)')
  19. patterndefault - A translated default pattern (e.g., t('[cat]/[title].html'))
  20. placeholders - An array whose keys consist of the translated placeholders
  21. which will appear in patterns (e.g., t('[title]')) and values are
  22. the translated description of the placeholders (e.g.,
  23. t('The title of the node, with spaces and punctuation.')
  24. patternitems - For modules which need to express multiple patterns
  25. (for example, the node module supports a separate pattern for each
  26. node type), an array whose keys consist of identifiers for each
  27. pattern (e.g., the node type name) and values consist of the
  28. translated label for the pattern
  29. supportsfeeds - Modules which support RSS feeds should set this to the
  30. string that's appended to a path for its feed (usually 'feed') , so
  31. when administrators enable "Create feed aliases" an alias for this
  32. content type's feed will be generated in addition to the base alias.
  33. bulkname - For modules which support a bulk update operation, the
  34. translated label for the action (e.g., t('Bulk update node paths'))
  35. bulkdescr - For modules which support a bulk update operation, a
  36. translated, more thorough description of what the operation will do
  37. (e.g., t('Generate aliases for all existing nodes which do not already have aliases.'))
  38. 2 - $alias = pathauto_create_alias($module, $op, $placeholders, $src, $type=NULL)
  39. At the appropriate time (usually when a new item is being created for
  40. which a generated alias is desired), call pathauto_create_alias() to
  41. generate and create the alias.
  42. $module - The name of your module (e.g., 'node')
  43. $op - Operation being performed on the item ('insert', 'update', or
  44. 'bulkupdate')
  45. $placeholders - An array whose keys consist of the translated placeholders
  46. which appear in patterns and values are the "clean" values to be
  47. substituted into the pattern. Call pathauto_cleanstring() on any
  48. values which you do not know to be purely alphanumeric, to substitute
  49. any non-alphanumerics with the user's designated separator. Note that
  50. if the pattern has multiple slash-separated components (e.g., [catpath]),
  51. pathauto_cleanstring() should be called for each component, not the
  52. complete string.
  53. Example: $placeholders[t('[title]')] = pathauto_cleanstring($node->title);
  54. $src - The "real" URI of the content to be aliased (e.g., "node/$node->nid")
  55. $type - For modules which provided patternitems in hook_autopath(),
  56. the relevant identifier for the specific item to be aliased (e.g.,
  57. $node->type)
  58. pathauto_create_alias() returns the alias that was created.
  59. 3 - Bulk update function
  60. If a module supports bulk updating of aliases, it must provide a
  61. function of this form, to be called by pathauto when the corresponding
  62. checkbox is selected and the settings page submitted:
  63. function _pathauto_bulkupdate()
  64. The function should iterate over the content items controlled by the
  65. module, calling pathauto_create_alias() for each one. It is
  66. recommended that the function report on its success (e.g., with a
  67. count of created aliases) via drupal_set_message().
  68. Modules that extend node and/or taxonomy
  69. Many contributed Drupal modules extend the core node and taxonomy
  70. modules. To extend pathauto patterns to support their extensions, they
  71. may implement the pathauto_node and pathauto_taxonomy hooks.
  72. To do so, implement the function _pathauto_node (or _taxonomy),
  73. accepting the arguments $op and $node (or $term). Two operations are
  74. supported:
  75. $op = 'placeholders' - return an array keyed on placeholder strings
  76. (e.g., t('[eventyyyy]')) valued with descriptions (e.g. t('The year the
  77. event starts.')).
  78. $op = 'values' - return an array keyed on placeholder strings, valued
  79. with the "clean" actual value for the passed node or category (e.g.,
  80. pathauto_cleanstring(date('M', $eventstart)));
  81. See contrib/pathauto_node_event.inc for an example of extending node
  82. patterns.