xmlsitemap_taxonomy.module 9.0 KB

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