xmlsitemap.api.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the XML sitemap module.
  5. *
  6. * @ingroup xmlsitemap
  7. */
  8. /**
  9. * @addtogroup hooks
  10. * @{
  11. */
  12. /**
  13. * Provide information on the type of links this module provides.
  14. *
  15. * @see hook_entity_info()
  16. * @see hook_entity_info_alter()
  17. */
  18. function hook_xmlsitemap_link_info() {
  19. return array(
  20. 'mymodule' => array(
  21. 'label' => 'My module',
  22. 'base table' => 'mymodule',
  23. 'entity keys' => array(
  24. // Primary ID key on {base table}.
  25. 'id' => 'myid',
  26. // Subtype key on {base table}.
  27. 'bundle' => 'mysubtype',
  28. ),
  29. 'path callback' => 'mymodule_path',
  30. 'bundle label' => t('Subtype name'),
  31. 'bundles' => array(
  32. 'mysubtype1' => array(
  33. 'label' => t('My subtype 1'),
  34. 'admin' => array(
  35. 'real path' => 'admin/settings/mymodule/mysubtype1/edit',
  36. 'access arguments' => array('administer mymodule'),
  37. ),
  38. 'xmlsitemap' => array(
  39. 'status' => XMLSITEMAP_STATUS_DEFAULT,
  40. 'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
  41. ),
  42. ),
  43. ),
  44. 'xmlsitemap' => array(
  45. // Callback function to take an array of IDs and save them as sitemap
  46. // links.
  47. 'process callback' => '',
  48. // Callback function used in batch API for rebuilding all links.
  49. 'rebuild callback' => '',
  50. // Callback function called from the XML sitemap settings page.
  51. 'settings callback' => '',
  52. ),
  53. ),
  54. );
  55. }
  56. /**
  57. * Act on a sitemap link being inserted or updated.
  58. *
  59. * This hook is currently invoked from xmlsitemap_node_node_update() before
  60. * the node sitemap link is saved to the database with revoked access until
  61. * the node permissions are checked in the cron.
  62. *
  63. * @param array $link
  64. * An array with the data of the sitemap link.
  65. * @param array $context
  66. * An optional context array containing data related to the link.
  67. */
  68. function hook_xmlsitemap_link_presave_alter(array &$link, array $context) {
  69. if ($link['type'] == 'mymodule') {
  70. $link['priority'] += 0.5;
  71. }
  72. }
  73. /**
  74. * Alter the data of a sitemap link before the link is saved.
  75. *
  76. * @param array $link
  77. * An array with the data of the sitemap link.
  78. * @param array $context
  79. * An optional context array containing data related to the link.
  80. */
  81. function hook_xmlsitemap_link_alter(array &$link, array $context) {
  82. if ($link['type'] == 'mymodule') {
  83. $link['priority'] += 0.5;
  84. }
  85. }
  86. /**
  87. * Inform modules that an XML sitemap link has been created.
  88. *
  89. * @param array $link
  90. * Associative array defining an XML sitemap link as passed into
  91. * xmlsitemap_link_save().
  92. * @param array $context
  93. * An optional context array containing data related to the link.
  94. *
  95. * @see hook_xmlsitemap_link_update()
  96. */
  97. function hook_xmlsitemap_link_insert(array $link, array $context) {
  98. db_insert('mytable')
  99. ->fields(array(
  100. 'link_type' => $link['type'],
  101. 'link_id' => $link['id'],
  102. 'link_status' => $link['status'],
  103. ))
  104. ->execute();
  105. }
  106. /**
  107. * Inform modules that an XML sitemap link has been updated.
  108. *
  109. * @param array $link
  110. * Associative array defining an XML sitemap link as passed into
  111. * xmlsitemap_link_save().
  112. * @param array $context
  113. * An optional context array containing data related to the link.
  114. *
  115. * @see hook_xmlsitemap_link_insert()
  116. */
  117. function hook_xmlsitemap_link_update(array $link, array $context) {
  118. db_update('mytable')
  119. ->fields(array(
  120. 'link_type' => $link['type'],
  121. 'link_id' => $link['id'],
  122. 'link_status' => $link['status'],
  123. ))
  124. ->execute();
  125. }
  126. /**
  127. * Respond to XML sitemap link clearing and rebuilding.
  128. *
  129. * @param array $types
  130. * An array of link types that are being rebuilt.
  131. * @param bool $save_custom
  132. * If links with overridden status and/or priority are being removed or not.
  133. */
  134. function hook_xmlsitemap_rebuild_clear(array $types, $save_custom) {
  135. db_delete('mytable')
  136. ->condition('link_type', $types, 'IN')
  137. ->execute();
  138. }
  139. /**
  140. * Respond to XML sitemap regeneration.
  141. */
  142. function hook_xmlsitemap_regenerate_finished() {
  143. }
  144. /**
  145. * Index links for the XML sitemaps.
  146. */
  147. function hook_xmlsitemap_index_links($limit) {
  148. }
  149. /**
  150. * Provide information about contexts available to XML sitemap.
  151. *
  152. * @see hook_xmlsitemap_context_info_alter()
  153. */
  154. function hook_xmlsitemap_context_info() {
  155. $info['vocabulary'] = array(
  156. 'label' => t('Vocabulary'),
  157. 'summary callback' => 'mymodule_xmlsitemap_vocabulary_context_summary',
  158. 'default' => 0,
  159. );
  160. return $info;
  161. }
  162. /**
  163. * Alter XML sitemap context info.
  164. *
  165. * @see hook_xmlsitemap_context_info()
  166. */
  167. function hook_xmlsitemap_context_info_alter(&$info) {
  168. $info['vocabulary']['label'] = t('Site vocabularies');
  169. }
  170. /**
  171. * Provide information about the current context on the site.
  172. *
  173. * @see hook_xmlsitemap_context_alter()
  174. */
  175. function hook_xmlsitemap_context() {
  176. $context = array();
  177. if ($vid = mymodule_get_current_vocabulary()) {
  178. $context['vocabulary'] = $vid;
  179. }
  180. return $context;
  181. }
  182. /**
  183. * Alter the current context information.
  184. *
  185. * @see hook_xmlsitemap_context()
  186. */
  187. function hook_xmlsitemap_context_alter(&$context) {
  188. if (user_access('administer taxonomy')) {
  189. unset($context['vocabulary']);
  190. }
  191. }
  192. /**
  193. * Provide options for the url() function based on an XML sitemap context.
  194. */
  195. function hook_xmlsitemap_context_url_options(array $context) {
  196. }
  197. /**
  198. * Alter the url() options based on an XML sitemap context.
  199. */
  200. function hook_xmlsitemap_context_url_options_alter(array &$options, array $context) {
  201. }
  202. /**
  203. * Alter the content added to an XML sitemap for an individual element.
  204. *
  205. * This hooks is called when the module is generating the XML content for the
  206. * sitemap and allows other modules to alter existing or add additional XML data
  207. * for any element by adding additional key value paris to the $element array.
  208. *
  209. * The key in the element array is then used as the name of the XML child
  210. * element to add and the value is the value of that element. For example:
  211. *
  212. * @code $element['video:title'] = 'Big Ponycorn'; @endcode
  213. *
  214. * Would result in a child element like <video:title>Big Ponycorn</video:title>
  215. * being added to the sitemap for this particular link.
  216. *
  217. * @param array $element
  218. * The element that will be converted to XML for the link.
  219. * @param array $link
  220. * An array of properties providing context about the link that we are
  221. * generating an XML element for.
  222. * @param object $sitemap
  223. * The sitemap that is currently being generated.
  224. */
  225. function hook_xmlsitemap_element_alter(array &$element, array $link, $sitemap) {
  226. if ($link['subtype'] === 'video') {
  227. $node = node_load($link['id']);
  228. $element['video:video'] = array(
  229. 'video:title' => check_plain($node->title),
  230. 'video:description' => isset($node->body[LANGUAGE_NONE][0]['summary']) ? check_plain($node->body[LANGUAGE_NONE][0]['summary']) : check_plain($node->body[LANGUAGE_NONE][0]['value']),
  231. 'video:live' => 'no',
  232. );
  233. }
  234. }
  235. /**
  236. * Alter the attributes used for the root element of the XML sitemap.
  237. *
  238. * For example add an xmlns:video attribute:
  239. *
  240. * @code
  241. * <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  242. * xmlns:video="https://www.google.com/schemas/sitemap-video/1.1">
  243. * @endcode
  244. *
  245. * @param array $attributes
  246. * An associative array of attributes to use in the root element of an XML
  247. * sitemap.
  248. * @param object $sitemap
  249. * The sitemap that is currently being generated.
  250. */
  251. function hook_xmlsitemap_root_attributes_alter(array &$attributes, $sitemap) {
  252. $attributes['xmlns:video'] = 'https://www.google.com/schemas/sitemap-video/1.1';
  253. }
  254. /**
  255. * Alter the query selecting data from {xmlsitemap} during sitemap generation.
  256. *
  257. * @param QueryAlterableInterface $query
  258. * A Query object describing the composite parts of a SQL query.
  259. *
  260. * @see hook_query_TAG_alter()
  261. */
  262. function hook_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
  263. $sitemap = $query->getMetaData('sitemap');
  264. if (!empty($sitemap->context['vocabulary'])) {
  265. $node_condition = db_and();
  266. $node_condition->condition('type', 'taxonomy_term');
  267. $node_condition->condition('subtype', $sitemap->context['vocabulary']);
  268. $normal_condition = db_and();
  269. $normal_condition->condition('type', 'taxonomy_term', '<>');
  270. $condition = db_or();
  271. $condition->condition($node_condition);
  272. $condition->condition($normal_condition);
  273. $query->condition($condition);
  274. }
  275. }
  276. /**
  277. * Provide information about XML sitemap bulk operations.
  278. */
  279. function hook_xmlsitemap_sitemap_operations() {
  280. }
  281. /**
  282. * Respond to XML sitemap deletion.
  283. *
  284. * This hook is invoked from xmlsitemap_sitemap_delete_multiple() after the XML
  285. * sitemap has been removed from the table in the database.
  286. *
  287. * @param object $sitemap
  288. * The XML sitemap object that was deleted.
  289. */
  290. function hook_xmlsitemap_sitemap_delete(stdClass $sitemap) {
  291. db_delete('mytable')
  292. ->condition('smid', $sitemap->smid, '=')
  293. ->execute();
  294. }
  295. /**
  296. * @} End of "addtogroup hooks".
  297. */