uc_catalog.pathauto.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Pathauto hooks.
  5. */
  6. /**
  7. * Implements hook_pathauto().
  8. */
  9. function uc_catalog_pathauto($op) {
  10. switch ($op) {
  11. case 'settings':
  12. $settings = array();
  13. $settings['module'] = 'uc_catalog';
  14. $settings['token_type'] = 'term';
  15. $settings['groupheader'] = t('Catalog paths');
  16. $settings['patterndescr'] = t('Pattern for catalog pages');
  17. $settings['patterndefault'] = 'catalog/[term:name]';
  18. $settings['supportsfeeds'] = 'feed';
  19. $settings['batch_update_callback'] = 'uc_catalog_pathauto_bulkupdate';
  20. $settings['batch_file'] = drupal_get_path('module', 'uc_catalog') . '/uc_catalog.pathauto.inc';
  21. return (object) $settings;
  22. }
  23. }
  24. /**
  25. * Implements hook_pathauto_bulkupdate().
  26. *
  27. * Generate aliases for all categories without aliases.
  28. */
  29. function uc_catalog_pathauto_bulkupdate(&$context) {
  30. if (!isset($context['sandbox']['current'])) {
  31. $context['sandbox']['count'] = 0;
  32. $context['sandbox']['current'] = 0;
  33. }
  34. $catalog_vid = variable_get('uc_catalog_vid', 0);
  35. $query = db_select('taxonomy_term_data', 'td');
  36. $query->leftJoin('url_alias', 'ua', "CONCAT('catalog/', td.tid) = ua.source");
  37. $query->addField('td', 'tid');
  38. $query->isNull('ua.source');
  39. $query->condition('vid', $catalog_vid);
  40. $query->orderBy('td.tid');
  41. $query->addTag('pathauto_bulk_update');
  42. $query->addMetaData('entity', 'taxonomy_term');
  43. // Get the total amount of items to process.
  44. if (!isset($context['sandbox']['total'])) {
  45. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  46. // If there are no nodes to update, the stop immediately.
  47. if (!$context['sandbox']['total']) {
  48. $context['finished'] = 1;
  49. return;
  50. }
  51. }
  52. $query->range(0, 25);
  53. $tids = $query->execute()->fetchCol();
  54. $terms = taxonomy_term_load_multiple($tids);
  55. $count = 0;
  56. $placeholders = array();
  57. foreach ($terms as $category) {
  58. $count = _uc_catalog_pathauto_alias($category, 'bulkupdate') + $count;
  59. }
  60. $context['sandbox']['count'] += count($tids);
  61. $context['sandbox']['current'] = max($tids);
  62. $context['message'] = t('Updated alias for term @tid.', array('@tid' => end($tids)));
  63. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  64. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  65. }
  66. }
  67. /**
  68. * Creates aliases for taxonomy objects.
  69. *
  70. * @param $category
  71. * A taxonomy object.
  72. */
  73. function _uc_catalog_pathauto_alias($category, $op) {
  74. module_load_include('inc', 'pathauto');
  75. $count = 0;
  76. if ($category->vid == variable_get('uc_catalog_vid', 0)) {
  77. // Check that the term has its bundle, which is the vocabulary's machine name.
  78. if (!isset($category->vocabulary_machine_name)) {
  79. $vocabulary = taxonomy_vocabulary_load($term->vid);
  80. $category->vocabulary_machine_name = $vocabulary->machine_name;
  81. }
  82. $src = uc_catalog_path($category);
  83. if ($alias = pathauto_create_alias('uc_catalog', $op, $src, array('term' => $category), $category->vocabulary_machine_name)) {
  84. $count++;
  85. }
  86. }
  87. return $count;
  88. }
  89. /**
  90. * Implements hook_path_alias_types().
  91. */
  92. function uc_catalog_path_alias_types() {
  93. return array('catalog/' => t('Catalog pages'));
  94. }