-
Notifications
You must be signed in to change notification settings - Fork 41
/
pathauto.pathauto.inc
329 lines (286 loc) · 11.2 KB
/
pathauto.pathauto.inc
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
<?php
/**
* @file
* Pathauto integration for core modules.
*
* @ingroup pathauto
*/
use Drupal\Core\Language\Language;
/**
* Implements hook_path_alias_types().
*
* Used primarily by the bulk delete form.
*/
function pathauto_path_alias_types() {
$objects['user/'] = t('Users');
$objects['node/'] = t('Content');
if (\Drupal::moduleHandler()->moduleExists('taxonomy')) {
$objects['taxonomy/term/'] = t('Taxonomy terms');
}
if (\Drupal::moduleHandler()->moduleExists('forum')) {
$objects['forum/'] = t('Forums');
}
return $objects;
}
/**
* Implements hook_pathauto().
*
* This function is empty so that the other core module implementations can be
* defined in this file. This is because in pathauto_module_implements_alter()
* we add pathauto to be included first. The module system then peforms a
* check on any subsequent run if this function still exists. If this does not
* exist, than this file will not get included and the core implementations
* will never get run.
*
* @see pathauto_module_implements_alter()
*/
function pathauto_pathauto() {
// Empty hook; see the above comment.
}
/**
* Implements hook_pathauto().
*/
function node_pathauto($op) {
switch ($op) {
case 'settings':
$settings = array();
$settings['module'] = 'node';
$settings['token_type'] = 'node';
$settings['groupheader'] = t('Content paths');
$settings['patterndescr'] = t('Default path pattern (applies to all content types with blank patterns below)');
$settings['patterndefault'] = 'content/[node:title]';
$settings['batch_update_callback'] = 'node_pathauto_bulk_update_batch_process';
$settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
$languages = array();
if (\Drupal::moduleHandler()->moduleExists('locale')) {
$languages = array(Language::LANGCODE_NOT_SPECIFIED => t('language neutral')) + \Drupal::languageManager()->getLanguages('name');
}
foreach (node_type_get_names() as $node_type => $node_name) {
if (count($languages) && \Drupal::moduleHandler()->moduleExists('content_translation') && \Drupal::service('content_translation.manager')->isEnabled('node', $node_type)) {
$settings['patternitems'][$node_type] = t('Default path pattern for @node_type (applies to all @node_type content types with blank patterns below)', array('@node_type' => $node_name));
foreach ($languages as $lang_code => $lang_name) {
$settings['patternitems'][$node_type . '_' . $lang_code] = t('Pattern for all @language @node_type paths', array('@node_type' => $node_name, '@language' => $lang_name));
}
}
else {
$settings['patternitems'][$node_type] = t('Pattern for all @node_type paths', array('@node_type' => $node_name));
}
}
return (object) $settings;
default:
break;
}
}
/**
* Batch processing callback; Generate aliases for nodes.
*/
function node_pathauto_bulk_update_batch_process(&$context) {
if (!isset($context['sandbox']['current'])) {
$context['sandbox']['count'] = 0;
$context['sandbox']['current'] = 0;
}
$query = db_select('node', 'n');
$query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source");
$query->addField('n', 'nid');
$query->isNull('ua.source');
$query->condition('n.nid', $context['sandbox']['current'], '>');
$query->orderBy('n.nid');
$query->addTag('pathauto_bulk_update');
$query->addMetaData('entity', 'node');
// Get the total amount of items to process.
if (!isset($context['sandbox']['total'])) {
$context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
// If there are no nodes to update, the stop immediately.
if (!$context['sandbox']['total']) {
$context['finished'] = 1;
return;
}
}
$query->range(0, 25);
$nids = $query->execute()->fetchCol();
pathauto_node_update_alias_multiple($nids, 'bulkupdate');
$context['sandbox']['count'] += count($nids);
$context['sandbox']['current'] = max($nids);
$context['message'] = t('Updated alias for node @nid.', array('@nid' => end($nids)));
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
}
}
/**
* Implements hook_pathauto().
*/
function taxonomy_pathauto($op) {
switch ($op) {
case 'settings':
$settings = array();
$settings['module'] = 'taxonomy_term';
$settings['token_type'] = 'term';
$settings['groupheader'] = t('Taxonomy term paths');
$settings['patterndescr'] = t('Default path pattern (applies to all vocabularies with blank patterns below)');
$settings['patterndefault'] = '[term:vocabulary]/[term:name]';
$settings['batch_update_callback'] = 'taxonomy_pathauto_bulk_update_batch_process';
$settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
$vocabularies = entity_load_multiple('taxonomy_vocabulary');
if (count($vocabularies)) {
$settings['patternitems'] = array();
foreach ($vocabularies as $vid => $vocabulary) {
/*if ($vid == variable_get('forum_nav_vocabulary', '')) {
// Skip the forum vocabulary.
continue;
}*/
$settings['patternitems'][$vocabulary->id()] = t('Pattern for all %vocab-name paths', array('%vocab-name' => $vocabulary->label()));
}
}
return (object) $settings;
default:
break;
}
}
/**
* Batch processing callback; Generate aliases for taxonomy terms.
*/
function taxonomy_pathauto_bulk_update_batch_process(&$context) {
if (!isset($context['sandbox']['current'])) {
$context['sandbox']['count'] = 0;
$context['sandbox']['current'] = 0;
}
$query = db_select('taxonomy_term_data', 'td');
$query->leftJoin('url_alias', 'ua', "CONCAT('taxonomy/term/', td.tid) = ua.source");
$query->addField('td', 'tid');
$query->isNull('ua.source');
$query->condition('td.tid', $context['sandbox']['current'], '>');
// Exclude the forums terms.
if ($forum_vid = 'forums') {
$query->condition('td.vid', $forum_vid, '<>');
}
$query->orderBy('td.tid');
$query->addTag('pathauto_bulk_update');
$query->addMetaData('entity', 'taxonomy_term');
// Get the total amount of items to process.
if (!isset($context['sandbox']['total'])) {
$context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
// If there are no nodes to update, the stop immediately.
if (!$context['sandbox']['total']) {
$context['finished'] = 1;
return;
}
}
$query->range(0, 25);
$tids = $query->execute()->fetchCol();
pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
$context['sandbox']['count'] += count($tids);
$context['sandbox']['current'] = max($tids);
$context['message'] = t('Updated alias for term @tid.', array('@tid' => end($tids)));
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
}
}
/**
* Implements hook_pathauto() for forum module.
*/
function forum_pathauto($op) {
switch ($op) {
case 'settings':
$settings = array();
$settings['module'] = 'forum';
$settings['token_type'] = 'term';
$settings['groupheader'] = t('Forum paths');
$settings['patterndescr'] = t('Pattern for forums and forum containers');
$settings['patterndefault'] = '[term:vocabulary]/[term:name]';
$settings['batch_update_callback'] = 'forum_pathauto_bulk_update_batch_process';
$settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
return (object) $settings;
default:
break;
}
}
/**
* Batch processing callback; Generate aliases for forums.
*/
function forum_pathauto_bulk_update_batch_process(&$context) {
if (!isset($context['sandbox']['current'])) {
$context['sandbox']['count'] = 0;
$context['sandbox']['current'] = 0;
}
$query = db_select('taxonomy_term_data', 'td');
$query->leftJoin('url_alias', 'ua', "CONCAT('forum/', td.tid) = ua.source");
$query->addField('td', 'tid');
$query->isNull('ua.source');
$query->condition('td.tid', $context['sandbox']['current'], '>');
$query->condition('td.vid', 'forums');
$query->orderBy('td.tid');
$query->addTag('pathauto_bulk_update');
$query->addMetaData('entity', 'taxonomy_term');
// Get the total amount of items to process.
if (!isset($context['sandbox']['total'])) {
$context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
// If there are no nodes to update, the stop immediately.
if (!$context['sandbox']['total']) {
$context['finished'] = 1;
return;
}
}
$query->range(0, 25);
$tids = $query->execute()->fetchCol();
pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
$context['sandbox']['count'] += count($tids);
$context['sandbox']['current'] = max($tids);
$context['message'] = t('Updated alias for forum @tid.', array('@tid' => end($tids)));
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
}
}
/**
* Implements hook_pathauto().
*/
function user_pathauto($op) {
switch ($op) {
case 'settings':
$settings = array();
$settings['module'] = 'user';
$settings['token_type'] = 'user';
$settings['groupheader'] = t('User paths');
$settings['patterndescr'] = t('Pattern for user account page paths');
$settings['patterndefault'] = 'users/[user:name]';
$settings['batch_update_callback'] = 'user_pathauto_bulk_update_batch_process';
$settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
return (object) $settings;
default:
break;
}
}
/**
* Batch processing callback; Generate aliases for users.
*/
function user_pathauto_bulk_update_batch_process(&$context) {
if (!isset($context['sandbox']['current'])) {
$context['sandbox']['count'] = 0;
$context['sandbox']['current'] = 0;
}
$query = db_select('users', 'u');
$query->leftJoin('url_alias', 'ua', "CONCAT('user/', u.uid) = ua.source");
$query->addField('u', 'uid');
$query->isNull('ua.source');
$query->condition('u.uid', $context['sandbox']['current'], '>');
$query->orderBy('u.uid');
$query->addTag('pathauto_bulk_update');
$query->addMetaData('entity', 'user');
// Get the total amount of items to process.
if (!isset($context['sandbox']['total'])) {
$context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
// If there are no nodes to update, the stop immediately.
if (!$context['sandbox']['total']) {
$context['finished'] = 1;
return;
}
}
$query->range(0, 25);
$uids = $query->execute()->fetchCol();
pathauto_user_update_alias_multiple($uids, 'bulkupdate', array());
$context['sandbox']['count'] += count($uids);
$context['sandbox']['current'] = max($uids);
$context['message'] = t('Updated alias for user @uid.', array('@uid' => end($uids)));
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
}
}