xmlsitemap_taxonomy.module 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Implements hook_entity_info_alter().
  4. */
  5. function xmlsitemap_taxonomy_entity_info_alter(&$entity_info) {
  6. $entity_info['taxonomy_term']['bundle label'] = t('Vocabulary');
  7. $entity_info['taxonomy_term']['xmlsitemap'] = array(
  8. 'process callback' => 'xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links',
  9. );
  10. }
  11. /**
  12. * Implements hook_xmlsitemap_link_info_alter().
  13. */
  14. function xmlsitemap_taxonomy_xmlsitemap_link_info_alter(&$link_info) {
  15. foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
  16. // Adjust the edit path to the *real* edit path.
  17. $link_info['taxonomy_term']['bundles'][$machine_name]['admin']['path'] .= '/edit';
  18. $link_info['taxonomy_term']['bundles'][$machine_name]['admin']['real path'] .= '/edit';
  19. }
  20. }
  21. /**
  22. * Implements hook_cron().
  23. *
  24. * Process old taxonomy terms not found in the {xmlsitemap} table.
  25. */
  26. function xmlsitemap_taxonomy_cron() {
  27. xmlsitemap_taxonomy_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
  28. }
  29. /**
  30. * Implements hook_xmlsitemap_index_links().
  31. */
  32. function xmlsitemap_taxonomy_xmlsitemap_index_links($limit) {
  33. if ($bundles = xmlsitemap_get_link_type_enabled_bundles('taxonomy_term')) {
  34. $tids = db_query_range("SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_vocabulary} tv USING (vid) LEFT JOIN {xmlsitemap} x ON x.type = 'taxonomy_term' AND t.tid = x.id WHERE x.id IS NULL AND tv.machine_name IN (:bundles) ORDER BY t.tid DESC", 0, $limit, array(':bundles' => $bundles))->fetchCol();
  35. xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links($tids);
  36. }
  37. }
  38. /**
  39. * Process taxonomy term sitemap links.
  40. *
  41. * @param $tids
  42. * An array of taxonomy term IDs.
  43. */
  44. function xmlsitemap_taxonomy_xmlsitemap_process_taxonomy_term_links(array $tids) {
  45. $terms = taxonomy_term_load_multiple($tids);
  46. foreach ($terms as $term) {
  47. $link = xmlsitemap_taxonomy_create_link($term);
  48. xmlsitemap_link_save($link);
  49. }
  50. }
  51. /**
  52. * Implements hook_form_FORM_ID_alter().
  53. *
  54. * @see taxonomy_form_vocabulary()
  55. * @see xmlsitemap_add_link_bundle_settings()
  56. */
  57. function xmlsitemap_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
  58. if (in_array('taxonomy_vocabulary_confirm_delete_submit', $form['#submit'])) {
  59. // If this is the delete form, do not add our form elements.
  60. return;
  61. }
  62. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  63. xmlsitemap_add_link_bundle_settings($form, $form_state, 'taxonomy_term', $form['#vocabulary']->machine_name);
  64. }
  65. /**
  66. * Implements hook_form_FORM_ID_alter().
  67. */
  68. function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state) {
  69. if ($form['name']['#type'] == 'value') {
  70. // If this is the delete form, do not add our form elements.
  71. return;
  72. }
  73. // Add the link options.
  74. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  75. xmlsitemap_add_form_link_options($form, 'taxonomy_term', $form['#term']['vocabulary_machine_name'], $form['tid']['#value']);
  76. }
  77. /**
  78. * Implements hook_taxonomy_vocabulary_insert().
  79. */
  80. function xmlsitemap_taxonomy_vocabulary_insert(stdClass $vocabulary) {
  81. if (isset($vocabulary->xmlsitemap)) {
  82. xmlsitemap_link_bundle_settings_save('taxonomy_term', $vocabulary->machine_name, $vocabulary->xmlsitemap);
  83. }
  84. }
  85. /**
  86. * Implements hook_taxonomy_vocabulary_update().
  87. */
  88. function xmlsitemap_taxonomy_vocabulary_update(stdClass $vocabulary) {
  89. if (isset($vocabulary->xmlsitemap)) {
  90. xmlsitemap_link_bundle_settings_save('taxonomy_term', $vocabulary->machine_name, $vocabulary->xmlsitemap);
  91. }
  92. }
  93. /**
  94. * Implements hook_taxonomy_term_insert() {
  95. */
  96. function xmlsitemap_taxonomy_term_insert(stdClass $term) {
  97. $link = xmlsitemap_taxonomy_create_link($term);
  98. xmlsitemap_link_save($link);
  99. }
  100. /**
  101. * Implements hook_taxonomy_term_update() {
  102. */
  103. function xmlsitemap_taxonomy_term_update(stdClass $term) {
  104. $link = xmlsitemap_taxonomy_create_link($term);
  105. xmlsitemap_link_save($link);
  106. }
  107. /**
  108. * Implements hook_taxonomy_term_delete() {
  109. */
  110. function xmlsitemap_taxonomy_term_delete(stdClass $term) {
  111. xmlsitemap_link_delete('taxonomy_term', $term->tid);
  112. }
  113. /**
  114. * Implements hook_field_extra_fields().
  115. */
  116. function xmlsitemap_taxonomy_field_extra_fields() {
  117. $extras = array();
  118. foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
  119. $extras['taxonomy_term'][$machine_name]['form']['xmlsitemap'] = array(
  120. 'label' => t('XML sitemap'),
  121. 'description' => t('XML sitemap module element'),
  122. 'weight' => 30,
  123. );
  124. }
  125. return $extras;
  126. }
  127. /**
  128. * Create a sitemap link from a taxonomy term.
  129. *
  130. * @param $term
  131. * A taxonomy term object.
  132. * @return
  133. * An array representing a sitemap link.
  134. */
  135. function xmlsitemap_taxonomy_create_link(stdClass &$term) {
  136. if (!isset($term->xmlsitemap)) {
  137. $term->xmlsitemap = array();
  138. if ($term->tid && $link = xmlsitemap_link_load('taxonomy_term', $term->tid)) {
  139. $term->xmlsitemap = $link;
  140. }
  141. }
  142. $settings = xmlsitemap_link_bundle_load('taxonomy_term', $term->vocabulary_machine_name);
  143. $uri = entity_uri('taxonomy_term', $term);
  144. $term->xmlsitemap += array(
  145. 'id' => $term->tid,
  146. 'type' => 'taxonomy_term',
  147. 'subtype' => $term->vocabulary_machine_name,
  148. 'status' => $settings['status'],
  149. 'status_default' => $settings['status'],
  150. 'status_override' => 0,
  151. 'priority' => $settings['priority'],
  152. 'priority_default' => $settings['priority'],
  153. 'priority_override' => 0,
  154. );
  155. // The following values must always be checked because they are volatile.
  156. // @todo How can/should we check taxonomy term access?
  157. $term->xmlsitemap['loc'] = $uri['path'];
  158. $term->xmlsitemap['access'] = 1;
  159. $term->xmlsitemap['language'] = isset($term->language) ? $term->language : LANGUAGE_NONE;
  160. return $term->xmlsitemap;
  161. }
  162. /**
  163. * Calculate the priority of a taxonomy term based on depth and weight.
  164. */
  165. //function xmlsitemap_taxonomy_calculate_term_priority(stdClass $term) {
  166. // // Calculate priority.
  167. // // Min weight = -128
  168. // // Max weight = 127
  169. // // Max depth = ?
  170. //}
  171. /**
  172. * Find the tree depth of a taxonomy term.
  173. *
  174. * @param $term
  175. * A taxonomy term object.
  176. * @return
  177. * The tree depth of the term.
  178. */
  179. function xmlsitemap_taxonomy_get_term_depth(stdClass $term) {
  180. static $depths = array();
  181. if (!isset($depths[$term->tid])) {
  182. if ($parent = db_query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = %d", $term->tid)->fetchField()) {
  183. // If the term has a parent, the term's depth is the parent's depth + 1.
  184. if (!isset($depths[$parent])) {
  185. $depths[$parent] = xmlsitemap_taxonomy_get_term_depth($parent);
  186. }
  187. $depths[$term->tid] = $depths[$parent] + 1;
  188. }
  189. else {
  190. // Term has no parents, so depth is 0.
  191. $depths[$term->tid] = 0;
  192. }
  193. }
  194. return $depths[$term->tid];
  195. }
  196. /**
  197. * Find the number of nodes that are associated with a taxonomy term.
  198. *
  199. * @param $term
  200. * A taxonomy term object.
  201. * @return
  202. * The number of nodes associated with the term.
  203. */
  204. function xmlsitemap_taxonomy_get_node_count(stdClass $term) {
  205. // @todo Use db_rewrite_sql() w/ switch user.
  206. return db_query_range("SELECT COUNT(ti.nid) FROM {taxonomy_index} ti LEFT JOIN {node n} USING (nid) WHERE ti.tid = :tid AND n.status = 1", 0, 1, array(':tid' => $term->tid))->fetchField();
  207. }
  208. /**
  209. * Implements hook_entity_query_alter().
  210. *
  211. * @todo Remove when http://drupal.org/node/1054162 is fixed.
  212. */
  213. function xmlsitemap_taxonomy_entity_query_alter($query) {
  214. $conditions = &$query->entityConditions;
  215. // Alter taxonomy term queries only.
  216. if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) {
  217. // We can only support the operators that are explicit in values.
  218. if (in_array($conditions['bundle']['operator'], array(NULL, '=', '!=', 'IN', 'NOT IN'))) {
  219. $vids = array();
  220. // Convert vocabulary machine names to vocabulary IDs.
  221. if (is_array($conditions['bundle']['value'])) {
  222. foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) {
  223. $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
  224. $vids[] = $vocabulary->vid;
  225. }
  226. }
  227. else {
  228. $vocabulary = taxonomy_vocabulary_machine_name_load($conditions['bundle']['value']);
  229. $vids = $vocabulary->vid;
  230. }
  231. $query->propertyCondition('vid', $vids, $conditions['bundle']['operator']);
  232. unset($conditions['bundle']);
  233. }
  234. }
  235. }