forked from md-systems/pathauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.txt
149 lines (117 loc) · 6.62 KB
/
API.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
This document explains how to provide "Pathauto integration" in a
module. You need this if you would like to provide additional tokens
or if your module has paths and you wish to have them automatically
aliased. The simplest integration is just to provide tokens so we
cover that first. More advanced integration requires an
implementation of hook_pathauto to provide a settings form.
It may be helpful to review some examples of integration from the
pathauto_node.inc, pathauto_taxonomy.inc, and pathauto_user.inc files.
==================
1 - Providing additional tokens
==================
If all you want is to enable tokens for your module you will simply
need to implement two functions:
hook_token_values
hook_token_list
See the token.module and it's API.txt for more information about this
process.
If the token is intended to generate a path expected to contain slashes,
the token name must end in 'path', 'path-raw' or 'alias'. This indicates to
Pathauto that the slashes should not be removed from the replacement value.
When an object is created (whether it is a node or a user or a
taxonomy term) the data that Pathauto hands to the token_values in the
$object is in a specific format. This is the format that most people
write code to handle. However, during edits and bulk updates the data
may be in a totally different format. So, if you are writing a
hook_token_values implementation to add special tokens, be sure to
test creation, edit, and bulk update cases to make sure your code will
handle it.
==================
2 - Settings hook - To create aliases for your module
==================
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('Content path settings')
patterndescr - The translated label for the default pattern (e.g.,
t('Default path pattern (applies to all content types with blank patterns below)')
patterndefault - A translated default pattern (e.g., t('[cat]/[title].html'))
token_type - The token type (e.g. 'node', 'user') that can be used.
patternitems - For modules which need to express multiple patterns
(for example, the node module supports a separate pattern for each
content type), an array whose keys consist of identifiers for each
pattern (e.g., the content type name) and values consist of the
translated label for the pattern
bulkname - For modules which support a bulk update operation, the
translated label for the action (e.g., t('Bulk update content 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 content items which do not already have aliases.'))
==================
2 - $alias = \Drupal::service('pathauto.manager')->createAlias($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 \Drupal::service('pathauto.manager')->createAlias() to
generate and create the alias. See the user, taxonomy, and nodeapi hook
implementations in pathauto.module for examples.
$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 \Drupal::service('pathauto.manager')->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., [term:path]),
\Drupal::service('pathauto.manager')->cleanString() should be called for each component, not the
complete string.
Example: $placeholders[t('[title]')] = \Drupal::service('pathauto.manager')->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)
\Drupal::service('pathauto.manager')->createAlias() 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 \Drupal::service('pathauto.manager')->createAlias() 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().
==================
4 - Bulk delete hook_path_alias_types()
==================
For modules that create new types of pages that can be aliased with pathauto, a
hook implementation is needed to allow the user to delete them all at once.
function hook_path_alias_types()
This hook returns an array whose keys match the beginning of the source paths
(e.g.: "node/", "user/", etc.) and whose values describe the type of page (e.g.:
"content", "users"). Like all displayed strings, these descriptionsshould be
localized with t(). Use % to match interior pieces of a path; "user/%/track". This
is a database wildcard, so be careful.
==================
Modules that extend node and/or taxonomy
==================
NOTE: this is basically not true any more. If you feel you need this file an issue.
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.,
\Drupal::service('pathauto.manager')->cleanString(date('M', $eventstart)));
See contrib/pathauto_node_event.inc for an example of extending node
patterns.