5 * Documentation for pathauto API.
8 use Drupal\Core\Language\Language;
11 * @todo Update for 8.x-1.x
13 * It may be helpful to review some examples of integration from
14 * pathauto.pathauto.inc.
16 * Pathauto works by using tokens in path patterns. Thus the simplest
17 * integration is just to provide tokens. Token support is provided by Drupal
18 * core. To provide additional token from your module, implement the following
21 * hook_tokens() - http://api.drupal.org/api/function/hook_tokens
22 * hook_token_info() - http://api.drupal.org/api/function/hook_token_info
24 * If you wish to provide pathauto integration for custom paths provided by your
25 * module, there are a few steps involved.
28 * Provide information required by pathauto for the settings form as well as
29 * bulk generation. See the documentation for hook_pathauto() for more
32 * 2. pathauto_create_alias()
33 * At the appropriate time (usually when a new item is being created for
34 * which a generated alias is desired), call pathauto_create_alias() with the
35 * appropriate parameters to generate and create the alias. See the user,
36 * taxonomy, and node hook implementations in pathauto.module for examples.
37 * Also see the documentation for pathauto_create_alias().
39 * 3. pathauto_path_delete_all()
40 * At the appropriate time (usually when an item is being deleted), call
41 * pathauto_path_delete_all() to remove any aliases that were created for the
42 * content being removed. See the documentation for
43 * pathauto_path_delete_all() for more details.
45 * 4. hook_path_alias_types()
46 * For modules that create new types of content that can be aliased with
47 * pathauto, a hook implementation is needed to allow the user to delete them
48 * all at once. See the documentation for hook_path_alias_types() below for
51 * There are other integration points with pathauto, namely alter hooks that
52 * allow you to change the data used by pathauto at various points in the
53 * process. See the below hook documentation for details.
57 * Alter pathauto alias type definitions.
59 * @param array &$definitions
60 * Alias type definitions.
62 function hook_path_alias_types_alter(array &$definitions) {
66 * Determine if a possible URL alias would conflict with any existing paths.
68 * Returning TRUE from this function will trigger pathauto_alias_uniquify() to
69 * generate a similar URL alias with a suffix to avoid conflicts.
71 * @param string $alias
72 * The potential URL alias.
73 * @param string $source
74 * The source path for the alias (e.g. 'node/1').
75 * @param string $langcode
76 * The language code for the alias (e.g. 'en').
79 * TRUE if $alias conflicts with an existing, reserved path, or FALSE/NULL if
80 * it does not match any reserved paths.
82 * @see pathauto_alias_uniquify()
84 function hook_pathauto_is_alias_reserved($alias, $source, $langcode) {
85 // Check our module's list of paths and return TRUE if $alias matches any of
87 return (bool) \Drupal::database()->query("SELECT 1 FROM {mytable} WHERE path = :path", [':path' => $alias])->fetchField();
91 * Alter the pattern to be used before an alias is generated by Pathauto.
93 * This hook will only be called if a default pattern is configured (on
94 * admin/config/search/path/patterns).
96 * @param string $pattern
97 * The alias pattern for Pathauto to pass to token_replace() to generate the
99 * @param array $context
100 * An associative array of additional options, with the following elements:
101 * - 'module': The module or entity type being aliased.
102 * - 'op': A string with the operation being performed on the object being
103 * aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
104 * - 'source': A string of the source path for the alias (e.g. 'node/1').
105 * - 'data': An array of keyed objects to pass to token_replace().
106 * - 'type': The sub-type or bundle of the object being aliased.
107 * - 'language': A string of the language code for the alias (e.g. 'en').
108 * This can be altered by reference.
110 function hook_pathauto_pattern_alter(&$pattern, array $context) {
111 // Switch out any [node:created:*] tokens with [node:updated:*] on update.
112 if ($context['module'] == 'node' && ($context['op'] == 'update')) {
113 $pattern = preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern);
118 * Alter Pathauto-generated aliases before saving.
120 * @param string $alias
121 * The automatic alias after token replacement and strings cleaned.
122 * @param array $context
123 * An associative array of additional options, with the following elements:
124 * - 'module': The module or entity type being aliased.
125 * - 'op': A string with the operation being performed on the object being
126 * aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
127 * - 'source': A string of the source path for the alias (e.g. 'node/1').
128 * This can be altered by reference.
129 * - 'data': An array of keyed objects to pass to token_replace().
130 * - 'type': The sub-type or bundle of the object being aliased.
131 * - 'language': A string of the language code for the alias (e.g. 'en').
132 * This can be altered by reference.
133 * - 'pattern': A string of the pattern used for aliasing the object.
135 function hook_pathauto_alias_alter(&$alias, array &$context) {
136 // Add a suffix so that all aliases get saved as 'content/my-title.html'
139 // Force all aliases to be saved as language neutral.
140 $context['language'] = Language::LANGCODE_NOT_SPECIFIED;
144 * Alter the list of punctuation characters for Pathauto control.
146 * @param $punctuation
147 * An array of punctuation to be controlled by Pathauto during replacement
148 * keyed by punctuation name. Each punctuation record should be an array
149 * with the following key/value pairs:
150 * - value: The raw value of the punctuation mark.
151 * - name: The human-readable name of the punctuation mark. This must be
152 * translated using t() already.
154 function hook_pathauto_punctuation_chars_alter(array &$punctuation) {
155 // Add the trademark symbol.
156 $punctuation['trademark'] = array('value' => '™', 'name' => t('Trademark symbol'));
158 // Remove the dollar sign.
159 unset($punctuation['dollar']);