forked from md-systems/pathauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathauto.module
362 lines (319 loc) · 12 KB
/
pathauto.module
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* @defgroup pathauto Pathauto: Automatically generates aliases for content
*
* The Pathauto module automatically generates path aliases for various kinds of
* content (nodes, categories, users) without requiring the user to manually
* specify the path alias. This allows you to get aliases like
* /category/my-node-title.html instead of /node/123. The aliases are based upon
* a "pattern" system which the administrator can control.
*/
/**
* @file
* Main file for the Pathauto module, which automatically generates aliases for content.
*
* @ingroup pathauto
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\Language;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
// @todo Hack until hook_module_implements_alter() works again, see
// https://www.drupal.org/node/2353357
require_once __DIR__ . '/pathauto.pathauto.inc';
/**
* The default ignore word list.
*/
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');
/**
* Implements hook_hook_info().
*/
function pathauto_hook_info() {
$hooks = array(
'pathauto',
'path_alias_types',
'pathauto_pattern_alter',
'pathauto_alias_alter',
'pathauto_is_alias_reserved',
);
return array_fill_keys($hooks, array('group' => 'pathauto'));
}
/**
* Implements hook_module_implements_alter().
*
* Adds pathauto support for core modules.
*/
function pathauto_module_implements_alter(&$implementations, $hook) {
if (in_array($hook, array('pathauto', 'path_alias_types'))) {
$modules = array('node', 'taxonomy', 'user', 'forum');
foreach ($modules as $module) {
if (\Drupal::moduleHandler()->moduleExists($module)) {
$implementations[$module] = TRUE;
}
}
// Move pathauto.module to get included first since it is responsible for
// other modules.
unset($implementations['pathauto']);
$implementations = array_merge(array('pathauto' => 'pathauto'), $implementations);
}
}
/**
* Implements hook_help().
*/
function pathauto_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.pathauto':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
$output .= '<h3>' . t('Settings') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
$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' => \Drupal::service('pathauto.alias_storage_helper')->getAliasSchemaMaxlength())) . '</dd>';
$output .= '</dl>';
return $output;
case 'pathauto.bulk.update.form':
$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>';
return $output;
}
}
/**
* Load an URL alias pattern by entity, bundle, and language.
*
* @param $entity
* An entity (e.g. node, taxonomy, user, etc.)
* @param $bundle
* A bundle (e.g. content type, vocabulary ID, etc.)
* @param $language
* A language code, defaults to the LANGUAGE_NONE constant.
*/
function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = Language::LANGCODE_NOT_SPECIFIED) {
$config = \Drupal::configFactory()->get('pathauto.pattern');
$patterns = &drupal_static(__FUNCTION__, array());
$pattern_id = "$entity:$bundle:$language";
if (!isset($patterns[$pattern_id])) {
$variables = array();
if ($language != Language::LANGCODE_NOT_SPECIFIED) {
$variables[] = "{$entity}.{$bundle}.{$language}";
}
if ($bundle) {
$variables[] = "{$entity}.{$bundle}._default";
}
$variables[] = "{$entity}._default";
foreach ($variables as $variable) {
if ($pattern = trim($config->get($variable))) {
break;
}
}
$patterns[$pattern_id] = $pattern;
}
return $patterns[$pattern_id];
}
/**
* Delete multiple URL aliases.
*
* Intent of this is to abstract a potential path_delete_multiple() function
* for Drupal 7 or 8.
*
* @param $pids
* An array of path IDs to delete.
*/
function pathauto_path_delete_multiple($pids) {
foreach ($pids as $pid) {
\Drupal::service('path.alias_storage')->delete(array('pid' => $pid));
}
}
/**
* Delete an URL alias and any of its sub-paths.
*
* Given a source like 'node/1' this function will delete any alias that have
* that specific source or any sources that match 'node/1/%'.
*
* @param $source
* An string with a source URL path.
*/
function pathauto_path_delete_all($source) {
$sql = "SELECT pid FROM {url_alias} WHERE source = :source OR source LIKE :source_wildcard";
$pids = db_query($sql, array(':source' => $source, ':source_wildcard' => $source . '/%'))->fetchCol();
if ($pids) {
pathauto_path_delete_multiple($pids);
}
}
/**
* Delete an entity URL alias and any of its sub-paths.
*
* This function also checks to see if the default entity URI is different from
* the current entity URI and will delete any of the default aliases.
*
* @param EntityInterface $entity
* An entity object.
* @param string $default_uri
* The optional default uri path for the entity.
*/
function pathauto_entity_path_delete_all(EntityInterface $entity, $default_uri = NULL) {
pathauto_path_delete_all($entity->getSystemPath());
if (isset($default_uri) && $entity->getSystemPath() != $default_uri) {
pathauto_path_delete_all($default_uri);
}
}
/**
* Implements hook_entity_bundle_rename().
*/
function pathauto_entity_bundle_rename($entity_type_id, $bundle_old, $bundle_new) {
$config = \Drupal::config('pathauto.pattern');
$bundle_settings = $config->get($entity_type_id);
if (isset($bundle_settings[$bundle_old])) {
$bundle_settings[$bundle_new] = $bundle_settings[$bundle_old];
unset($bundle_settings[$bundle_old]);
$config->set($entity_type_id, $bundle_settings);
$config->save();
}
}
/**
* Implements hook__entity_bundle_delete().
*/
function pathauto_entity_bundle_delete($entity_type, $bundle) {
$config = \Drupal::config('pathauto.pattern');
$config->clear($entity_type . '.' . $bundle);
$config->save();
}
/**
* Implements hook_entity_presave().
*/
function pathauto_entity_presave($entity) {
if (!($entity instanceof ContentEntityInterface) || $entity->hasField('path')) {
return;
}
// About to be saved (before insert/update)
if (!empty($entity->path->pathauto) && isset($entity->path->old_alias)
&& $entity->path->alias == '' && $entity->path->old_alias != '') {
/*
* There was an old alias, but when pathauto_perform_alias was checked
* the javascript disabled the textbox which led to an empty value being
* submitted. Restoring the old path-value here prevents the Path module
* from deleting any old alias before Pathauto gets control.
*/
$entity->path->alias = $entity->path->old_alias;
}
// Help prevent errors with progromatically creating entities by defining
// path['alias'] as an empty string.
// @see http://drupal.org/node/1328180
// @see http://drupal.org/node/1576552
if (isset($entity->path->pathauto) && !isset($entity->path->alias)) {
$entity->path->alias = '';
}
}
/**
* Implements hook_entity_insert().
*/
function pathauto_entity_insert(EntityInterface $entity) {
\Drupal::service('pathauto.manager')->updateAlias($entity, 'insert');
}
/**
* Implements hook_entity_update().
*/
function pathauto_entity_update(EntityInterface $entity) {
\Drupal::service('pathauto.manager')->updateAlias($entity, 'update');
}
/**
* Update the URL aliases for multiple nodes.
*
* @param array $nids
* An array of node IDs.
* @param string $op
* Operation being performed on the nodes ('insert', 'update' or
* 'bulkupdate').
* @param array $options
* An optional array of additional options.
*/
function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) {
$options += array('message' => FALSE);
$nodes = Node::loadMultiple($nids);
foreach ($nodes as $node) {
\Drupal::service('pathauto.manager')->updateAlias($node, $op, $options);
}
if (!empty($options['message'])) {
drupal_set_message(\Drupal::translation()->formatPlural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
}
}
/**
* Update the URL aliases for multiple taxonomy terms.
*
* @param array $tids
* An array of term IDs.
* @param string $op
* Operation being performed on the nodes ('insert', 'update' or
* 'bulkupdate').
* @param array $options
* An optional array of additional options.
*/
function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
$options += array('message' => FALSE);
$terms = entity_load_multiple('taxonomy_term', $tids);
foreach ($terms as $term) {
\Drupal::service('pathauto.manager')->updateAlias($term, $op, $options);
}
if (!empty($options['message'])) {
drupal_set_message(\Drupal::translation()->formatPlural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
}
}
/**
* Update the URL aliases for multiple user accounts.
*
* @param array $uids
* An array of user account IDs.
* @param string $op
* Operation being performed on the accounts ('insert', 'update' or
* 'bulkupdate').
* @param array $options
* An optional array of additional options.
*/
function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) {
$options += array('message' => FALSE);
$accounts = User::loadMultiple($uids);
foreach ($accounts as $account) {
\Drupal::service('pathauto.manager')->updateAlias($account, $op, $options);
}
if (!empty($options['message'])) {
drupal_set_message(\Drupal::translation()->formatPlural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
}
}
/**
* Implements hook_field_info_alter().
*/
function pathauto_field_info_alter(&$info) {
$info['path']['class'] = '\Drupal\pathauto\PathautoItem';
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function pathauto_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if (isset($fields['path']) && $fields['path']->getType() == 'path') {
$fields['path']->setDisplayOptions('form', array(
'type' => 'pathauto',
'weight' => 30,
));
}
}
/**
* Implements hook_entity_base_field_info().
*/
function pathauto_entity_base_field_info(EntityTypeInterface $entity_type) {
// @todo: Make this configurable and/or remove if
// https://drupal.org/node/476294 is resolved.
if ($entity_type->id() === 'user') {
$fields['path'] = BaseFieldDefinition::create('path')
->setCustomStorage(TRUE)
->setLabel(t('URL alias'))
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'type' => 'pathauto',
'weight' => 30,
))
->setDisplayConfigurable('form', TRUE);
return $fields;
}
}