summaryrefslogtreecommitdiffstats
path: root/pathauto.module
blob: 9bb132d081008835d9eeebdea7deda3d5c63632a (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
<?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
 */

/**
 * 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', 'blog');
    foreach ($modules as $module) {
      if (module_exists($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($path, $arg) {
  switch ($path) {
    case 'admin/help#pathauto':
      module_load_include('inc', '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' => _pathauto_get_schema_alias_maxlength())) . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'admin/config/search/path/update_bulk':
      $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;
  }
}

/**
 * Implements hook_permission().
 */
function pathauto_permission() {
  return array(
    'administer pathauto' => array(
      'title' => t('Administer pathauto'),
      'description' => t('Allows a user to configure patterns for automated aliases and bulk delete URL-aliases.'),
    ),
    'notify of path changes' => array(
      'title' => t('Notify of Path Changes'),
      'description' => t('Determines whether or not users are notified.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function pathauto_menu() {
  $items['admin/config/search/path/patterns'] = array(
    'title' => 'Patterns',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_patterns_form'),
    'access arguments' => array('administer pathauto'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'pathauto.admin.inc',
  );
  $items['admin/config/search/path/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_settings_form'),
    'access arguments' => array('administer pathauto'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 20,
    'file' => 'pathauto.admin.inc',
  );
  $items['admin/config/search/path/update_bulk'] = array(
    'title' => 'Bulk generate',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_bulk_update_form'),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 30,
    'file' => 'pathauto.admin.inc',
  );
  $items['admin/config/search/path/delete_bulk'] = array(
    'title' => 'Delete aliases',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pathauto_admin_delete'),
    'access arguments' => array('administer url aliases'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 40,
    'file' => 'pathauto.admin.inc',
  );

  return $items;
}

/**
 * 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_NONE) {
  $patterns = &drupal_static(__FUNCTION__, array());

  $pattern_id = "$entity:$bundle:$language";
  if (!isset($patterns[$pattern_id])) {
    $variables = array();
    if ($language != LANGUAGE_NONE) {
      $variables[] = "pathauto_{$entity}_{$bundle}_{$language}_pattern";
    }
    if ($bundle) {
      $variables[] = "pathauto_{$entity}_{$bundle}_pattern";
    }
    $variables[] = "pathauto_{$entity}_pattern";

    foreach ($variables as $variable) {
      if ($pattern = trim(variable_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) {
    path_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 $entity_type
 *   A string with the entity type.
 * @param $entity
 *   An entity object.
 * @param $default_uri
 *   The optional default uri path for the entity.
 */
function pathauto_entity_path_delete_all($entity_type, $entity, $default_uri = NULL) {
  $uri = entity_uri($entity_type, $entity);
  pathauto_path_delete_all($uri['path']);
  if (isset($default_uri) && $uri['path'] != $default_uri) {
    pathauto_path_delete_all($default_uri);
  }
}

/**
 * Implements hook_field_attach_rename_bundle().
 *
 * Respond to machine name changes for pattern variables.
 */
function pathauto_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  $variables = db_select('variable', 'v')
    ->fields('v', array('name'))
    ->condition('name', db_like("pathauto_{$entity_type}_{$bundle_old}_") . '%', 'LIKE')
    ->execute()
    ->fetchCol();
  foreach ($variables as $variable) {
    $value = variable_get($variable, '');
    variable_del($variable);
    $variable = strtr($variable, array("{$entity_type}_{$bundle_old}" => "{$entity_type}_{$bundle_new}"));
    variable_set($variable, $value);
  }
}

/**
 * Implements hook_field_attach_delete_bundle().
 *
 * Respond to sub-types being deleted, their patterns can be removed.
 */
function pathauto_field_attach_delete_bundle($entity_type, $bundle) {
  $variables = db_select('variable', 'v')
    ->fields('v', array('name'))
    ->condition('name', db_like("pathauto_{$entity_type}_{$bundle}_") . '%', 'LIKE')
    ->execute()
    ->fetchCol();
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

/**
 * Implements hook_field_attach_form().
 *
 * Add the automatic alias form elements to an existing path form fieldset.
 */
function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);

  if (!isset($form['path'])) {
    // This entity must be supported by core's path.module first.
    // @todo Investigate removing this and supporting all fieldable entities.
    return;
  }
  else {
    // Taxonomy terms do not have an actual fieldset for path settings.
    // Merge in the defaults.
    $form['path'] += array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($form['path']['alias']),
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array('path-form'),
      ),
      '#access' => user_access('create url aliases') || user_access('administer url aliases'),
      '#weight' => 30,
      '#tree' => TRUE,
      '#element_validate' => array('path_form_element_validate'),
    );
  }

  $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $langcode);
  if (empty($pattern)) {
    return;
  }

  if (!isset($entity->path['pathauto'])) {
    if (!empty($id)) {
      module_load_include('inc', 'pathauto');
      $uri = entity_uri($entity_type, $entity);
      $pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array($entity_type => $entity), $bundle, $langcode);
      if ($pathauto_alias === FALSE) {
        // If Pathauto is not going to be able to generate an alias, then we
        // should not bother to show the checkbox since it wouldn't do anything.
        // Note that if a pattern does apply, but all the tokens currently
        // evaluate to empty strings, then $pathauto_alias would equal null and
        // not false.
        return;
      }
      else {
        $path = drupal_get_path_alias($uri['path'], $langcode);
        $entity->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);
      }
    }
    else {
      $entity->path['pathauto'] = TRUE;
    }
  }

  // Add JavaScript that will disable the path textfield when the automatic
  // alias checkbox is checked.
  $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]'] = array('checked' => TRUE);

  // Override path.module's vertical tabs summary.
  $form['path']['#attached']['js'] = array(
    'vertical-tabs' => drupal_get_path('module', 'pathauto') . '/pathauto.js'
  );

  $form['path']['pathauto'] = array(
    '#type' => 'checkbox',
    '#title' => t('Generate automatic URL alias'),
    '#default_value' => $entity->path['pathauto'],
    '#description' => t('Uncheck this to create a custom alias below.'),
    '#weight' => -1,
  );

  // Add a shortcut link to configure URL alias patterns.
  if (drupal_valid_path('admin/config/search/path/patterns')) {
    $form['path']['pathauto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/search/path/patterns');
  }

  if ($entity->path['pathauto'] && !empty($entity->old_alias) && empty($entity->path['alias'])) {
    $form['path']['alias']['#default_value'] = $entity->old_alias;
    $entity->path['alias'] = $entity->old_alias;
  }

  // For Pathauto to remember the old alias and prevent the Path module from
  // deleting it when Pathauto wants to preserve it.
  if (!empty($entity->path['alias'])) {
    $form['path']['old_alias'] = array(
      '#type' => 'value',
      '#value' => $entity->path['alias'],
    );
  }
}

/**
 * Implements hook_entity_presave().
 */
function pathauto_entity_presave($entity, $type) {
  // 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_action_info().
 */
function pathauto_action_info() {
  $info['pathauto_node_update_action'] = array(
    'type' => 'node',
    'label' => t('Update node alias'),
    'configurable' => FALSE,
    'triggers' => array(),
  );
  $info['pathauto_taxonomy_term_update_action'] = array(
    'type' => 'taxonomy_term',
    'label' => t('Update taxonomy term alias'),
    'configurable' => FALSE,
    'triggers' => array(),
  );
  $info['pathauto_user_update_action'] = array(
    'type' => 'user',
    'label' => t('Update user alias'),
    'configurable' => FALSE,
    'triggers' => array(),
  );

  return $info;
}

/**
 * Returns the language code of the given entity.
 *
 * Backward compatibility layer to ensure that installations running an older
 * version of core where entity_language() is not avilable do not break.
 *
 * @param string $entity_type
 *   An entity type.
 * @param object $entity
 *   An entity object.
 * @param bool $check_language_property
 *   A boolean if TRUE, will attempt to fetch the language code from
 *   $entity->language if the entity_language() function failed or does not
 *   exist. Default is TRUE.
 */
function pathauto_entity_language($entity_type, $entity, $check_language_property = TRUE) {
  $langcode = NULL;

  if (function_exists('entity_language')) {
    $langcode = entity_language($entity_type, $entity);
  }
  elseif ($check_language_property && !empty($entity->language)) {
    $langcode = $entity->language;
  }

  return !empty($langcode) ? $langcode : LANGUAGE_NONE;
}

function pathauto_is_alias_reserved($alias, $source, $langcode = LANGUAGE_NONE) {
  foreach (module_implements('pathauto_is_alias_reserved') as $module) {
    $result = module_invoke($module, 'pathauto_is_alias_reserved', $alias, $source, $langcode);
    if (!empty($result)) {
      // As soon as the first module says that an alias is in fact reserved,
      // then there is no point in checking the rest of the modules.
      return TRUE;
    }
  }

  return FALSE;
}

/**
 * Implements hook_pathauto_is_alias_reserved() on behalf of path.module.
 */
function path_pathauto_is_alias_reserved($alias, $source, $langcode) {
  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(
    ':source' => $source,
    ':alias' => $alias,
    ':language' => $langcode,
    ':language_none' => LANGUAGE_NONE,
  ))->fetchField();
}

/**
 * Implements hook_pathauto_is_alias_reserved().
 */
function pathauto_pathauto_is_alias_reserved($alias, $source, $langcode) {
  module_load_include('inc', 'pathauto');
  return _pathauto_path_is_callback($alias);
}

if (!function_exists('path_field_extra_fields')) {
/**
 * Implements hook_field_extra_fields() on behalf of path.module.
 *
 * Add support for the 'URL path settings' to be re-ordered by the user on the
 * 'Manage Fields' tab of content types and vocabularies.
 */
function path_field_extra_fields() {
  $info = array();

  foreach (node_type_get_types() as $node_type) {
    if (!isset($info['node'][$node_type->type]['form']['path'])) {
      $info['node'][$node_type->type]['form']['path'] = array(
        'label' => t('URL path settings'),
        'description' => t('Path module form elements'),
        'weight' => 30,
      );
    }
  }

  if (module_exists('taxonomy')) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $vocabulary) {
      if (!isset($info['taxonomy_term'][$vocabulary->machine_name]['form']['path'])) {
        $info['taxonomy_term'][$vocabulary->machine_name]['form']['path'] = array(
          'label' => t('URL path settings'),
          'description' => t('Path module form elements'),
          'weight' => 30,
        );
      }
    }
  }

  return $info;
}
}

/**
 * @name pathauto_node Pathauto integration for the core node module.
 * @{
 */

/**
 * Implements hook_node_insert().
 */
function pathauto_node_insert($node) {
  // @todo Remove the next line when http://drupal.org/node/1025870 is fixed.
  unset($node->uri);
  pathauto_node_update_alias($node, 'insert');
}

/**
 * Implements hook_node_update().
 */
function pathauto_node_update($node) {
  pathauto_node_update_alias($node, 'update');
}

/**
 * Implements hook_node_delete().
 */
function pathauto_node_delete($node) {
  pathauto_entity_path_delete_all('node', $node, "node/{$node->nid}");
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Add the Pathauto settings to the node form.
 */
function pathauto_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];
  $langcode = pathauto_entity_language('node', $node);
  pathauto_field_attach_form('node', $node, $form, $form_state, $langcode);
}

/**
 * Implements hook_node_operations().
 */
function pathauto_node_operations() {
  $operations['pathauto_update_alias'] = array(
    'label' => t('Update URL alias'),
    'callback' => 'pathauto_node_update_alias_multiple',
    'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  );
  return $operations;
}

/**
 * Update the URL aliases for an individual node.
 *
 * @param $node
 *   A node object.
 * @param $op
 *   Operation being performed on the node ('insert', 'update' or 'bulkupdate').
 * @param $options
 *   An optional array of additional options.
 */
function pathauto_node_update_alias(stdClass $node, $op, array $options = array()) {
  // Skip processing if the user has disabled pathauto for the node.
  if (isset($node->path['pathauto']) && empty($node->path['pathauto']) && empty($options['force'])) {
    return FALSE;
  }

  $options += array('language' => pathauto_entity_language('node', $node));

  // Skip processing if the node has no pattern.
  if (!pathauto_pattern_load_by_entity('node', $node->type, $options['language'])) {
    return FALSE;
  }

  module_load_include('inc', 'pathauto');
  $uri = entity_uri('node', $node);
  return pathauto_create_alias('node', $op, $uri['path'], array('node' => $node), $node->type, $options['language']);
}

/**
 * Update the URL aliases for multiple nodes.
 *
 * @param $nids
 *   An array of node IDs.
 * @param $op
 *   Operation being performed on the nodes ('insert', 'update' or
 *   'bulkupdate').
 * @param $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_load_multiple($nids);
  foreach ($nodes as $node) {
    pathauto_node_update_alias($node, $op, $options);
  }

  if (!empty($options['message'])) {
    drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
  }
}

/**
 * Update action wrapper for pathauto_node_update_alias().
 */
function pathauto_node_update_action($node, $context = array()) {
  pathauto_node_update_alias($node, 'bulkupdate', array('message' => TRUE));
}

/**
 * @} End of "name pathauto_node".
 */

/**
 * @name pathauto_taxonomy Pathauto integration for the core taxonomy module.
 * @{
 */

/**
 * Implements hook_taxonomy_term_insert().
 */
function pathauto_taxonomy_term_insert($term) {
  pathauto_taxonomy_term_update_alias($term, 'insert');
}

/**
 * Implements hook_taxonomy_term_update().
 */
function pathauto_taxonomy_term_update($term) {
  pathauto_taxonomy_term_update_alias($term, 'update', array('alias children' => TRUE));
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function pathauto_taxonomy_term_delete($term) {
  pathauto_entity_path_delete_all('taxonomy_term', $term, "taxonomy/term/{$term->tid}");
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add the Pathauto settings to the taxonomy term form.
 */
function pathauto_form_taxonomy_form_term_alter(&$form, $form_state) {
  $term = $form_state['term'];
  $langcode = pathauto_entity_language('taxonomy_term', $term);
  pathauto_field_attach_form('taxonomy_term', $term, $form, $form_state, $langcode);
}

/**
 * Update the URL aliases for an individual taxonomy term.
 *
 * @param $term
 *   A taxonomy term object.
 * @param $op
 *   Operation being performed on the term ('insert', 'update' or 'bulkupdate').
 * @param $options
 *   An optional array of additional options.
 */
function pathauto_taxonomy_term_update_alias(stdClass $term, $op, array $options = array()) {
  // Skip processing if the user has disabled pathauto for the term.
  if (isset($term->path['pathauto']) && empty($term->path['pathauto']) && empty($options['force'])) {
    return FALSE;
  }

  $module = 'taxonomy_term';
  if ($term->vid == variable_get('forum_nav_vocabulary', '')) {
    if (module_exists('forum')) {
      $module = 'forum';
    }
    else {
      return FALSE;
    }
  }

  // Check that the term has its bundle, which is the vocabulary's machine name.
  if (!isset($term->vocabulary_machine_name)) {
    $vocabulary = taxonomy_vocabulary_load($term->vid);
    $term->vocabulary_machine_name = $vocabulary->machine_name;
  }

  $options += array(
    'alias children' => FALSE,
    'language' => pathauto_entity_language('taxonomy_term', $term),
  );

  // Skip processing if the term has no pattern.
  if (!pathauto_pattern_load_by_entity($module, $term->vocabulary_machine_name)) {
    return FALSE;
  }

  module_load_include('inc', 'pathauto');
  $uri = entity_uri('taxonomy_term', $term);
  $result = pathauto_create_alias($module, $op, $uri['path'], array('term' => $term), $term->vocabulary_machine_name, $options['language']);

  if (!empty($options['alias children'])) {
    // For all children generate new aliases.
    $options['alias children'] = FALSE;
    unset($options['language']);
    foreach (taxonomy_get_tree($term->vid, $term->tid) as $subterm) {
      pathauto_taxonomy_term_update_alias($subterm, $op, $options);
    }
  }

  return $result;
}

/**
 * Update the URL aliases for multiple taxonomy terms.
 *
 * @param $tids
 *   An array of term IDs.
 * @param $op
 *   Operation being performed on the nodes ('insert', 'update' or
 *   'bulkupdate').
 * @param $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 = taxonomy_term_load_multiple($tids);
  foreach ($terms as $term) {
    pathauto_taxonomy_term_update_alias($term, $op, $options);
  }

  if (!empty($options['message'])) {
    drupal_set_message(format_plural(count($tids), 'Updated URL alias for 1 term.', 'Updated URL aliases for @count terms.'));
  }
}

/**
 * Update action wrapper for pathauto_taxonomy_term_update_alias().
 */
function pathauto_taxonomy_term_update_action($term, $context = array()) {
  pathauto_taxonomy_term_update_alias($term, 'bulkupdate', array('message' => TRUE));
}

/**
 * @} End of "name pathauto_taxonomy".
 */

/**
 * @name pathauto_user Pathauto integration for the core user and blog modules.
 * @{
 */

/**
 * Implements hook_user_insert().
 */
function pathauto_user_insert(&$edit, $account, $category) {
  pathauto_user_update_alias($account, 'insert');
}

/**
 * Implements hook_user_update().
 */
function pathauto_user_update(&$edit, $account, $category) {
  pathauto_user_update_alias($account, 'update');
}

/**
 * Implements hook_user_delete().
 */
function pathauto_user_delete($account) {
  pathauto_entity_path_delete_all('user', $account, "user/{$account->uid}");
  pathauto_path_delete_all("blog/{$account->uid}");
}

/**
 * Implements hook_user_operations().
 */
function pathauto_user_operations() {
  $operations['pathauto_update_alias'] = array(
    'label' => t('Update URL alias'),
    'callback' => 'pathauto_user_update_alias_multiple',
    'callback arguments' => array('bulkupdate', array('message' => TRUE)),
  );
  return $operations;
}

/**
 * Update the URL aliases for an individual user account.
 *
 * @param $account
 *   A user account object.
 * @param $op
 *   Operation being performed on the account ('insert', 'update' or
 *   'bulkupdate').
 * @param $options
 *   An optional array of additional options.
 */
function pathauto_user_update_alias(stdClass $account, $op, array $options = array()) {
  // Skip processing if the user has disabled pathauto for the account.
  if (isset($account->path['pathauto']) && empty($account->path['pathauto']) && empty($options['force'])) {
    return FALSE;
  }

  $options += array(
    'alias blog' => module_exists('blog'),
    // $user->language is not the user entity language, thus we need to skip
    // the property fallback check.
    'language' => pathauto_entity_language('user', $account, FALSE),
  );

  // Skip processing if the account has no pattern.
  if (!pathauto_pattern_load_by_entity('user', '', $options['language'])) {
    return FALSE;
  }

  module_load_include('inc', 'pathauto');
  $uri = entity_uri('user', $account);
  $return = pathauto_create_alias('user', $op, $uri['path'], array('user' => $account), NULL, $options['language']);

  // Because blogs are also associated with users, also generate the blog paths.
  if (!empty($options['alias blog'])) {
    pathauto_blog_update_alias($account, $op, $options);
  }

  return $return;
}

/**
 * Update the URL aliases for multiple user accounts.
 *
 * @param $uids
 *   An array of user account IDs.
 * @param $op
 *   Operation being performed on the accounts ('insert', 'update' or
 *   'bulkupdate').
 * @param $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_load_multiple($uids);
  foreach ($accounts as $account) {
    pathauto_user_update_alias($account, $op, $options);
  }

  if (!empty($options['message'])) {
    drupal_set_message(format_plural(count($uids), 'Updated URL alias for 1 user account.', 'Updated URL aliases for @count user accounts.'));
  }
}

/**
 * Update action wrapper for pathauto_user_update_alias().
 */
function pathauto_user_update_action($account, $context = array()) {
  pathauto_user_update_alias($account, 'bulkupdate', array('message' => TRUE));
}

/**
 * Update the blog URL aliases for an individual user account.
 *
 * @param $account
 *   A user account object.
 * @param $op
 *   Operation being performed on the blog ('insert', 'update' or
 *   'bulkupdate').
 * @param $options
 *   An optional array of additional options.
 */
function pathauto_blog_update_alias(stdClass $account, $op, array $options = array()) {
  // Skip processing if the blog has no pattern.
  if (!pathauto_pattern_load_by_entity('blog')) {
    return FALSE;
  }

  $options += array(
    'language' => LANGUAGE_NONE,
  );

  module_load_include('inc', 'pathauto');
  if (node_access('create', 'blog', $account)) {
    return pathauto_create_alias('blog', $op, "blog/{$account->uid}", array('user' => $account), NULL, $options['language']);
  }
  else {
    pathauto_path_delete_all("blog/{$account->uid}");
  }
}

/**
 * @} End of "name pathauto_user".
 */