Fixed example hook_pathauto_pattern_alter().
[project/pathauto.git] / pathauto.module
1 <?php
2
3 /**
4 * @defgroup pathauto Pathauto: Automatically generates aliases for content
5 *
6 * The Pathauto module automatically generates path aliases for various kinds of
7 * content (nodes, categories, users) without requiring the user to manually
8 * specify the path alias. This allows you to get aliases like
9 * /category/my-node-title.html instead of /node/123. The aliases are based upon
10 * a "pattern" system which the administrator can control.
11 */
12
13 /**
14 * @file
15 * Main file for the Pathauto module, which automatically generates aliases for content.
16 *
17 * @ingroup pathauto
18 */
19
20 /**
21 * The default ignore word list.
22 */
23 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');
24
25 /**
26 * Implements hook_hook_info().
27 */
28 function pathauto_hook_info() {
29 $hooks = array(
30 'pathauto',
31 'path_alias_types',
32 'pathauto_pattern_alter',
33 'pathauto_alias_alter',
34 'pathauto_is_alias_reserved',
35 );
36 return array_fill_keys($hooks, array('group' => 'pathauto'));
37 }
38
39 /**
40 * Implements hook_module_implements_alter().
41 *
42 * Adds pathauto support for core modules.
43 */
44 function pathauto_module_implements_alter(&$implementations, $hook) {
45 if (in_array($hook, array('pathauto', 'path_alias_types'))) {
46 $modules = array('node', 'taxonomy', 'user', 'forum', 'blog');
47 foreach ($modules as $module) {
48 if (module_exists($module)) {
49 $implementations[$module] = TRUE;
50 }
51 }
52 // Move pathauto.module to get included first since it is responsible for
53 // other modules.
54 unset($implementations['pathauto']);
55 $implementations = array_merge(array('pathauto' => 'pathauto'), $implementations);
56 }
57 }
58
59 /**
60 * Implements hook_help().
61 */
62 function pathauto_help($path, $arg) {
63 switch ($path) {
64 case 'admin/help#pathauto':
65 module_load_include('inc', 'pathauto');
66 $output = '<h3>' . t('About') . '</h3>';
67 $output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
68 $output .= '<h3>' . t('Settings') . '</h3>';
69 $output .= '<dl>';
70 $output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
71 $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' => _pathauto_get_schema_alias_maxlength())) . '</dd>';
72 $output .= '</dl>';
73 return $output;
74 case 'admin/config/search/path/update_bulk':
75 $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>';
76 return $output;
77 }
78 }
79
80 /**
81 * Implements hook_permission().
82 */
83 function pathauto_permission() {
84 return array(
85 'administer pathauto' => array(
86 'title' => t('Administer pathauto'),
87 'description' => t('Allows a user to configure patterns for automated aliases and bulk delete URL-aliases.'),
88 ),
89 'notify of path changes' => array(
90 'title' => t('Notify of Path Changes'),
91 'description' => t('Determines whether or not users are notified.'),
92 ),
93 );
94 }
95
96 /**
97 * Implements hook_menu().
98 */
99 function pathauto_menu() {
100 $items['admin/config/search/path/patterns'] = array(
101 'title' => 'Patterns',
102 'page callback' => 'drupal_get_form',
103 'page arguments' => array('pathauto_patterns_form'),
104 'access arguments' => array('administer pathauto'),
105 'type' => MENU_LOCAL_TASK,
106 'weight' => 10,
107 'file' => 'pathauto.admin.inc',
108 );
109 $items['admin/config/search/path/settings'] = array(
110 'title' => 'Settings',
111 'page callback' => 'drupal_get_form',
112 'page arguments' => array('pathauto_settings_form'),
113 'access arguments' => array('administer pathauto'),
114 'type' => MENU_LOCAL_TASK,
115 'weight' => 20,
116 'file' => 'pathauto.admin.inc',
117 );
118 $items['admin/config/search/path/update_bulk'] = array(
119 'title' => 'Bulk generate',
120 'page callback' => 'drupal_get_form',
121 'page arguments' => array('pathauto_bulk_update_form'),
122 'access arguments' => array('administer url aliases'),
123 'type' => MENU_LOCAL_TASK,
124 'weight' => 30,
125 'file' => 'pathauto.admin.inc',
126 );
127 $items['admin/config/search/path/delete_bulk'] = array(
128 'title' => 'Delete aliases',
129 'page callback' => 'drupal_get_form',
130 'page arguments' => array('pathauto_admin_delete'),
131 'access arguments' => array('administer url aliases'),
132 'type' => MENU_LOCAL_TASK,
133 'weight' => 40,
134 'file' => 'pathauto.admin.inc',
135 );
136
137 return $items;
138 }
139
140 /**
141 * Load an URL alias pattern by entity, bundle, and language.
142 *
143 * @param $entity
144 * An entity (e.g. node, taxonomy, user, etc.)
145 * @param $bundle
146 * A bundle (e.g. content type, vocabulary ID, etc.)
147 * @param $language
148 * A language code, defaults to the LANGUAGE_NONE constant.
149 */
150 function pathauto_pattern_load_by_entity($entity, $bundle = '', $language = LANGUAGE_NONE) {
151 $patterns = &drupal_static(__FUNCTION__, array());
152
153 $pattern_id = "$entity:$bundle:$language";
154 if (!isset($patterns[$pattern_id])) {
155 $variables = array();
156 if ($language != LANGUAGE_NONE) {
157 $variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern";
158 }
159 if ($bundle) {
160 $variables[] = "pathauto_{$entity}_{$bundle}_pattern";
161 }
162 $variables[] = "pathauto_{$entity}_pattern";
163
164 foreach ($variables as $variable) {
165 if ($pattern = trim(variable_get($variable, ''))) {
166 break;
167 }
168 }
169
170 $patterns[$pattern_id] = $pattern;
171 }
172
173 return $patterns[$pattern_id];
174 }
175
176 /**
177 * Delete multiple URL aliases.
178 *
179 * Intent of this is to abstract a potential path_delete_multiple() function
180 * for Drupal 7 or 8.
181 *
182 * @param $pids
183 * An array of path IDs to delete.
184 */
185 function pathauto_path_delete_multiple($pids) {
186 foreach ($pids as $pid) {
187 path_delete(array('pid' => $pid));
188 }
189 }
190
191 /**
192 * Delete an URL alias and any of its sub-paths.
193 *
194 * Given a source like 'node/1' this function will delete any alias that have
195 * that specific source or any sources that match 'node/1/%'.
196 *
197 * @param $source
198 * An string with a source URL path.
199 */
200 function pathauto_path_delete_all($source) {
201 $sql = "SELECT pid FROM {url_alias} WHERE source = :source OR source LIKE :source_wildcard";
202 $pids = db_query($sql, array(':source' => $source, ':source_wildcard' => $source . '/%'))->fetchCol();
203 if ($pids) {
204 pathauto_path_delete_multiple($pids);
205 }
206 }
207
208 /**
209 * Delete an entity URL alias and any of its sub-paths.
210 *
211 * This function also checks to see if the default entity URI is different from
212 * the current entity URI and will delete any of the default aliases.
213 *
214 * @param $entity_type
215 * A string with the entity type.
216 * @param $entity
217 * An entity object.
218 * @param $default_uri
219 * The optional default uri path for the entity.
220 */
221 function pathauto_entity_path_delete_all($entity_type, $entity, $default_uri = NULL) {
222 $uri = entity_uri($entity_type, $entity);
223 pathauto_path_delete_all($uri['path']);
224 if (isset($default_uri) && $uri['path'] != $default_uri) {
225 pathauto_path_delete_all($default_uri);
226 }
227 }
228
229 /**
230 * Implements hook_field_attach_rename_bundle().
231 *
232 * Respond to machine name changes for pattern variables.
233 */
234 function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
235 $variables = db_select('variable', 'v')
236 ->fields('v', array('name'))
237 ->condition('name', db_like("pathauto_{$entity_type}_{$bundle_old}_") . '%', 'LIKE')
238 ->execute()
239 ->fetchCol();
240 foreach ($variables as $variable) {
241 $value = variable_get($variable, '');
242 variable_del($variable);
243 $variable = strtr($variable, array("{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}"));
244 variable_set($variable, $value);
245 }
246 }
247
248 /**
249 * Implements hook_field_attach_delete_bundle().
250 *
251 * Respond to sub-types being deleted, their patterns can be removed.
252 */
253 function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
254 $variables = db_select('variable', 'v')
255 ->fields('v', array('name'))
256 ->condition('name', db_like("pathauto_{$entity_type}_{$bundle}_") . '%', 'LIKE')
257 ->execute()
258 ->fetchCol();
259 foreach ($variables as $variable) {
260 variable_del($variable);
261 }
262 }
263
264 /**
265 * Implements hook_field_attach_form().
266 *
267 * Add the automatic alias form elements to an existing path form fieldset.
268 */
269 function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
270 list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
271
272 if (!isset($form['path'])) {
273 // This entity must be supported by core's path.module first.
274 // @todo Investigate removing this and supporting all fieldable entities.
275 return;
276 }
277 else {
278 // Taxonomy terms do not have an actual fieldset for path settings.
279 // Merge in the defaults.
280 $form['path'] += array(
281 '#type' => 'fieldset',
282 '#title' => t('URL path settings'),
283 '#collapsible' => TRUE,
284 '#collapsed' => empty($form['path']['alias']),
285 '#group' => 'additional_settings',
286 '#attributes' => array(
287 'class' => array('path-form'),
288 ),
289 '#access' => user_access('create url aliases') || user_access('administer url aliases'),
290 '#weight' => 30,
291 '#tree' => TRUE,
292 '#element_validate' => array('path_form_element_validate'),
293 );
294 }
295
296 $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $langcode);
297 if (empty($pattern)) {
298 return;
299 }
300
301 if (!isset($entity->path['pathauto'])) {
302 if (!empty($id)) {
303 module_load_include('inc', 'pathauto');
304 $uri = entity_uri($entity_type, $entity);
305 $path = drupal_get_path_alias($uri['path'], $langcode);
306 $pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array($entity_type => $entity), $bundle, $langcode);
307 $entity->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);
308 }
309 else {
310 $entity->path['pathauto'] = TRUE;
311 }
312 }
313
314 // Add JavaScript that will disable the path textfield when the automatic
315 // alias checkbox is checked.
316 $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]'] = array('checked' => TRUE);
317
318 // Override path.module's vertical tabs summary.
319 $form['path']['#attached']['js'] = array(
320 'vertical-tabs' => drupal_get_path('module', 'pathauto') . '/pathauto.js'
321 );
322
323 $form['path']['pathauto'] = array(
324 '#type' => 'checkbox',
325 '#title' => t('Generate automatic URL alias'),
326 '#default_value' => $entity->path['pathauto'],
327 '#description' => t('Uncheck this to create a custom alias below.'),
328 '#weight' => -1,
329 );
330
331 // Add a shortcut link to configure URL alias patterns.
332 if (drupal_valid_path('admin/config/search/path/patterns')) {
333 $form['path']['pathauto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/search/path/patterns');
334 }
335
336 if ($entity->path['pathauto'] && !empty($entity->old_alias) && empty($entity->path['alias'])) {
337 $form['path']['alias']['#default_value'] = $entity->old_alias;
338 $entity->path['alias'] = $entity->old_alias;
339 }
340
341 // For Pathauto to remember the old alias and prevent the Path module from
342 // deleting it when Pathauto wants to preserve it.
343 if (!empty($entity->path['alias'])) {
344 $form['path']['old_alias'] = array(
345 '#type' => 'value',
346 '#value' => $entity->path['alias'],
347 );
348 }
349 }
350
351 /**
352 * Implements hook_entity_presave().
353 */
354 function pathauto_entity_presave($entity, $type) {
355 // About to be saved (before insert/update)
356 if (!empty($entity->path['pathauto']) && isset($entity->path['old_alias'])
357 && $entity->path['alias'] == '' && $entity->path['old_alias'] != '') {
358 /**
359 * There was an old alias, but when pathauto_perform_alias was checked
360 * the javascript disabled the textbox which led to an empty value being
361 * submitted. Restoring the old path-value here prevents the Path module
362 * from deleting any old alias before Pathauto gets control.
363 */
364 $entity->path['alias'] = $entity->path['old_alias'];
365 }
366
367 // Help prevent errors with progromatically creating entities by defining
368 // path['alias'] as an empty string.
369 // @see http://drupal.org/node/1328180
370 // @see http://drupal.org/node/1576552
371 if (isset($entity->path['pathauto']) && !isset($entity->path['alias'])) {
372 $entity->path['alias'] = '';
373 }
374 }
375
376 /**
377 * Implements hook_action_info().
378 */
379 function pathauto_action_info() {
380 $info['pathauto_node_update_action'] = array(
381 'type' => 'node',
382 'label' => t('Update node alias'),
383 'configurable' => FALSE,
384 'triggers' => array(),
385 );
386 $info['pathauto_taxonomy_term_update_action'] = array(
387 'type' => 'taxonomy_term',
388 'label' => t('Update taxonomy term alias'),
389 'configurable' => FALSE,
390 'triggers' => array(),
391 );
392 $info['pathauto_user_update_action'] = array(
393 'type' => 'user',
394 'label' => t('Update user alias'),
395 'configurable' => FALSE,
396 'triggers' => array(),
397 );
398
399 return $info;
400 }
401
402 /**
403 * Returns the language code of the given entity.
404 *
405 * Backward compatibility layer to ensure that installations running an older
406 * version of core where entity_language() is not avilable do not break.
407 *
408 * @param string $entity_type
409 * An entity type.
410 * @param object $entity
411 * An entity object.
412 * @param bool $check_language_property
413 * A boolean if TRUE, will attempt to fetch the language code from
414 * $entity->language if the entity_language() function failed or does not
415 * exist. Default is TRUE.
416 */
417 function pathauto_entity_language($entity_type, $entity, $check_language_property = TRUE) {
418 $langcode = NULL;
419
420 if (function_exists('entity_language')) {
421 $langcode = entity_language($entity_type, $entity);
422 }
423 elseif ($check_language_property && !empty($entity->language)) {
424 $langcode = $entity->language;
425 }
426
427 return !empty($langcode) ? $langcode : LANGUAGE_NONE;
428 }
429
430 function pathauto_is_alias_reserved($alias, $source, $langcode = LANGUAGE_NONE) {
431 foreach (module_implements('pathauto_is_alias_reserved') as $module) {
432 $result = module_invoke($module, 'pathauto_is_alias_reserved', $alias, $source, $langcode);
433 if (!empty($result)) {
434 // As soon as the first module says that an alias is in fact reserved,
435 // then there is no point in checking the rest of the modules.
436 return TRUE;
437 }
438 }
439
440 return FALSE;
441 }
442
443 /**
444 * Implements hook_pathauto_is_alias_reserved() on behalf of path.module.
445 */
446 function path_pathauto_is_alias_reserved($alias, $source, $langcode) {
447 return (bool) db_query_range("SELECT pid FROM {url_alias} WHERE source <> :source AND alias = :alias AND language IN (:language, :language_none) ORDER BY language DESC, pid DESC", 0, 1, array(
448 ':source' => $source,
449 ':alias' => $alias,
450 ':language' => $langcode,
451 ':language_none' => LANGUAGE_NONE,
452 ))->fetchField();
453 }
454
455 /**
456 * Implements hook_pathauto_is_alias_reserved().
457 */
458 function pathauto_pathauto_is_alias_reserved($alias, $source, $langcode) {
459 module_load_include('inc', 'pathauto');
460 return _pathauto_path_is_callback($alias);
461 }
462
463 if (!function_exists('path_field_extra_fields')) {
464 /**
465 * Implements hook_field_extra_fields() on behalf of path.module.
466 *
467 * Add support for the 'URL path settings' to be re-ordered by the user on the
468 * 'Manage Fields' tab of content types and vocabularies.
469 */
470 function path_field_extra_fields() {
471 $info = array();
472
473 foreach (node_type_get_types() as $node_type) {
474 if (!isset($info['node'][$node_type->type]['form']['path'])) {
475 $info['node'][$node_type->type]['form']['path'] = array(
476 'label' => t('URL path settings'),
477 'description' => t('Path module form elements'),
478 'weight' => 30,
479 );
480 }
481 }
482
483 if (module_exists('taxonomy')) {
484 $vocabularies = taxonomy_get_vocabularies();
485 foreach ($vocabularies as $vocabulary) {
486 if (!isset($info['taxonomy_term'][$vocabulary->machine_name]['form']['path'])) {
487 $info['taxonomy_term'][$vocabulary->machine_name]['form']['path'] = array(
488 'label' => t('URL path settings'),
489 'description' => t('Path module form elements'),
490 'weight' => 30,
491 );
492 }
493 }
494 }
495
496 return $info;
497 }
498 }
499
500 /**
501 * @name pathauto_node Pathauto integration for the core node module.
502 * @{
503 */
504
505 /**
506 * Implements hook_node_insert().
507 */
508 function pathauto_node_insert($node) {
509 // @todo Remove the next line when http://drupal.org/node/1025870 is fixed.
510 unset($node->uri);
511 pathauto_node_update_alias($node, 'insert');
512 }
513
514 /**
515 * Implements hook_node_update().
516 */
517 function pathauto_node_update($node) {
518 pathauto_node_update_alias($node, 'update');
519 }
520
521 /**
522 * Implements hook_node_delete().
523 */
524 function pathauto_node_delete($node) {
525 pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}");
526 }
527
528 /**
529 * Implements hook_form_BASE_FORM_ID_alter().
530 *
531 * Add the Pathauto settings to the node form.
532 */
533 function pathauto_form_node_form_alter(&$form, &$form_state) {
534 $node = $form_state['node'];
535 $langcode = pathauto_entity_language('node', $node);
536 pathauto_field_attach_form('node', $node, $form, $form_state, $langcode);
537 }
538
539 /**
540 * Implements hook_node_operations().
541 */
542 function pathauto_node_operations() {
543 $operations['pathauto_update_alias'] = array(
544 'label' => t('Update URL alias'),
545 'callback' => 'pathauto_node_update_alias_multiple',
546 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
547 );
548 return $operations;
549 }
550
551 /**
552 * Update the URL aliases for an individual node.
553 *
554 * @param $node
555 * A node object.
556 * @param $op
557 * Operation being performed on the node ('insert', 'update' or 'bulkupdate').
558 * @param $options
559 * An optional array of additional options.
560 */
561 function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) {
562 // Skip processing if the user has disabled pathauto for the node.
563 if (isset($node->path['pathauto']) && empty($node->path['pathauto']) && empty($options['force'])) {
564 return;
565 }
566
567 $options += array('language' => pathauto_entity_language('node', $node));
568
569 // Skip processing if the node has no pattern.
570 if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
571 return;
572 }
573
574 module_load_include('inc', 'pathauto');
575 $uri = entity_uri('node', $node);
576 return pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']);
577 }
578
579 /**
580 * Update the URL aliases for multiple nodes.
581 *
582 * @param $nids
583 * An array of node IDs.
584 * @param $op
585 * Operation being performed on the nodes ('insert', 'update' or
586 * 'bulkupdate').
587 * @param $options
588 * An optional array of additional options.
589 */
590 function pathauto_node_update_alias_multiple(array $nids, $op, array $options = array()) {
591 $options += array('message' => FALSE);
592
593 $nodes = node_load_multiple($nids);
594 foreach ($nodes as $node) {
595 pathauto_node_update_alias($node, $op, $options);
596 }
597
598 if (!empty($options['message'])) {
599 drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
600 }
601 }
602
603 /**
604 * Update action wrapper for pathauto_node_update_alias().
605 */
606 function pathauto_node_update_action($node, $context = array()) {
607 pathauto_node_update_alias($node, 'bulkupdate', array('message' => TRUE));
608 }
609
610 /**
611 * @} End of "name pathauto_node".
612 */
613
614 /**
615 * @name pathauto_taxonomy Pathauto integration for the core taxonomy module.
616 * @{
617 */
618
619 /**
620 * Implements hook_taxonomy_term_insert().
621 */
622 function pathauto_taxonomy_term_insert($term) {
623 pathauto_taxonomy_term_update_alias($term, 'insert');
624 }
625
626 /**
627 * Implements hook_taxonomy_term_update().
628 */
629 function pathauto_taxonomy_term_update($term) {
630 pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE));
631 }
632
633 /**
634 * Implements hook_taxonomy_term_delete().
635 */
636 function pathauto_taxonomy_term_delete($term) {
637 pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}");
638 }
639
640 /**
641 * Implements hook_form_FORM_ID_alter().
642 *
643 * Add the Pathauto settings to the taxonomy term form.
644 */
645 function pathauto_form_taxonomy_form_term_alter(&$form, $form_state) {
646 $term = $form_state['term'];
647 $langcode = pathauto_entity_language('taxonomy_term', $term);
648 pathauto_field_attach_form('taxonomy_term', $term, $form, $form_state, $langcode);
649 }
650
651 /**
652 * Update the URL aliases for an individual taxonomy term.
653 *
654 * @param $term
655 * A taxonomy term object.
656 * @param $op
657 * Operation being performed on the term ('insert', 'update' or 'bulkupdate').
658 * @param $options
659 * An optional array of additional options.
660 */
661 function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) {
662 // Skip processing if the user has disabled pathauto for the term.
663 if (isset($term->path['pathauto']) && empty($term->path['pathauto']) && empty($options['force'])) {
664 return;
665 }
666
667 $module = 'taxonomy_term';
668 if ($term->vid == variable_get('forum_nav_vocabulary', '')) {
669 if (module_exists('forum')) {
670 $module = 'forum';
671 }
672 else {
673 return;
674 }
675 }
676
677 // Check that the term has its bundle, which is the vocabulary's machine name.
678 if (!isset($term->vocabulary_machine_name)) {
679 $vocabulary = taxonomy_vocabulary_load($term->vid);
680 $term->vocabulary_machine_name = $vocabulary->machine_name;
681 }
682
683 $options += array(
684 'alias children' => FALSE,
685 'language' => pathauto_entity_language('taxonomy_term', $term),
686 );
687
688 // Skip processing if the term has no pattern.
689 if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) {
690 return;
691 }
692
693 module_load_include('inc', 'pathauto');
694 $uri = entity_uri('taxonomy_term', $term);
695 $result = pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']);
696
697 if (!empty($options['alias children'])) {
698 // For all children generate new aliases.
699 $options['alias children'] = FALSE;
700 unset($options['language']);
701 foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) {
702 pathauto_taxonomy_term_update_alias($subterm, $op, $options);
703 }
704 }
705
706 return $result;
707 }
708
709 /**
710 * Update the URL aliases for multiple taxonomy terms.
711 *
712 * @param $tids
713 * An array of term IDs.
714 * @param $op
715 * Operation being performed on the nodes ('insert', 'update' or
716 * 'bulkupdate').
717 * @param $options
718 * An optional array of additional options.
719 */
720 function pathauto_taxonomy_term_update_alias_multiple(array $tids, $op, array $options = array()) {
721 $options += array('message' => FALSE);
722
723 $terms = taxonomy_term_load_multiple($tids);
724 foreach ($terms as $term) {
725 pathauto_taxonomy_term_update_alias($term, $op, $options);
726 }
727
728 if (!empty($options['message'])) {
729 drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
730 }
731 }
732
733 /**
734 * Update action wrapper for pathauto_taxonomy_term_update_alias().
735 */
736 function pathauto_taxonomy_term_update_action($term, $context = array()) {
737 pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array('message' => TRUE));
738 }
739
740 /**
741 * @} End of "name pathauto_taxonomy".
742 */
743
744 /**
745 * @name pathauto_user Pathauto integration for the core user and blog modules.
746 * @{
747 */
748
749 /**
750 * Implements hook_user_insert().
751 */
752 function pathauto_user_insert(&$edit, $account, $category) {
753 pathauto_user_update_alias($account, 'insert');
754 }
755
756 /**
757 * Implements hook_user_update().
758 */
759 function pathauto_user_update(&$edit, $account, $category) {
760 pathauto_user_update_alias($account, 'update');
761 }
762
763 /**
764 * Implements hook_user_delete().
765 */
766 function pathauto_user_delete($account) {
767 pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}");
768 pathauto_path_delete_all("blog/{$account->uid}");
769 }
770
771 /**
772 * Implements hook_user_operations().
773 */
774 function pathauto_user_operations() {
775 $operations['pathauto_update_alias'] = array(
776 'label' => t('Update URL alias'),
777 'callback' => 'pathauto_user_update_alias_multiple',
778 'callback arguments' => array('bulkupdate', array('message' => TRUE)),
779 );
780 return $operations;
781 }
782
783 /**
784 * Update the URL aliases for an individual user account.
785 *
786 * @param $account
787 * A user account object.
788 * @param $op
789 * Operation being performed on the account ('insert', 'update' or
790 * 'bulkupdate').
791 * @param $options
792 * An optional array of additional options.
793 */
794 function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) {
795 // Skip processing if the user has disabled pathauto for the account.
796 if (isset($account->path['pathauto']) && empty($account->path['pathauto']) && empty($options['force'])) {
797 return;
798 }
799
800 $options += array(
801 'alias blog' => module_exists('blog'),
802 // $user->language is not the user entity language, thus we need to skip
803 // the property fallback check.
804 'language' => pathauto_entity_language('user', $account, FALSE),
805 );
806
807 // Skip processing if the account has no pattern.
808 if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
809 return;
810 }
811
812 module_load_include('inc', 'pathauto');
813 $uri = entity_uri('user', $account);
814 $return = pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']);
815
816 // Because blogs are also associated with users, also generate the blog paths.
817 if (!empty($options['alias blog'])) {
818 pathauto_blog_update_alias($account, $op, $options);
819 }
820
821 return $return;
822 }
823
824 /**
825 * Update the URL aliases for multiple user accounts.
826 *
827 * @param $uids
828 * An array of user account IDs.
829 * @param $op
830 * Operation being performed on the accounts ('insert', 'update' or
831 * 'bulkupdate').
832 * @param $options
833 * An optional array of additional options.
834 */
835 function pathauto_user_update_alias_multiple(array $uids, $op, array $options = array()) {
836 $options += array('message' => FALSE);
837
838 $accounts = user_load_multiple($uids);
839 foreach ($accounts as $account) {
840 pathauto_user_update_alias($account, $op, $options);
841 }
842
843 if (!empty($options['message'])) {
844 drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
845 }
846 }
847
848 /**
849 * Update action wrapper for pathauto_user_update_alias().
850 */
851 function pathauto_user_update_action($account, $context = array()) {
852 pathauto_user_update_alias($account, 'bulkupdate', array('message' => TRUE));
853 }
854
855 /**
856 * Update the blog URL aliases for an individual user account.
857 *
858 * @param $account
859 * A user account object.
860 * @param $op
861 * Operation being performed on the blog ('insert', 'update' or
862 * 'bulkupdate').
863 * @param $options
864 * An optional array of additional options.
865 */
866 function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) {
867 // Skip processing if the blog has no pattern.
868 if (!pathauto_pattern_load_by_entity('blog')) {
869 return;
870 }
871
872 $options += array(
873 'language' => LANGUAGE_NONE,
874 );
875
876 module_load_include('inc', 'pathauto');
877 if (node_access('create', 'blog', $account)) {
878 return pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']);
879 }
880 else {
881 pathauto_path_delete_all("blog/{$account->uid}");
882 }
883 }
884
885 /**
886 * @} End of "name pathauto_user".
887 */