xmlsitemap.api.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * Alter the data of a sitemap link before the link is saved.
  58. *
  59. * @param array $link
  60. * An array with the data of the sitemap link.
  61. * @param array $context
  62. * An optional context array containing data related to the link.
  63. */
  64. function hook_xmlsitemap_link_alter(array &$link, array $context) {
  65. if ($link['type'] == 'mymodule') {
  66. $link['priority'] += 0.5;
  67. }
  68. }
  69. /**
  70. * Inform modules that an XML sitemap link has been created.
  71. *
  72. * @param $link
  73. * Associative array defining an XML sitemap link as passed into
  74. * xmlsitemap_link_save().
  75. * @param array $context
  76. * An optional context array containing data related to the link.
  77. *
  78. * @see hook_xmlsitemap_link_update()
  79. */
  80. function hook_xmlsitemap_link_insert(array $link, array $context) {
  81. db_insert('mytable')
  82. ->fields(array(
  83. 'link_type' => $link['type'],
  84. 'link_id' => $link['id'],
  85. 'link_status' => $link['status'],
  86. ))
  87. ->execute();
  88. }
  89. /**
  90. * Inform modules that an XML sitemap link has been updated.
  91. *
  92. * @param $link
  93. * Associative array defining an XML sitemap link as passed into
  94. * xmlsitemap_link_save().
  95. * @param array $context
  96. * An optional context array containing data related to the link.
  97. *
  98. * @see hook_xmlsitemap_link_insert()
  99. */
  100. function hook_xmlsitemap_link_update(array $link, array $context) {
  101. db_update('mytable')
  102. ->fields(array(
  103. 'link_type' => $link['type'],
  104. 'link_id' => $link['id'],
  105. 'link_status' => $link['status'],
  106. ))
  107. ->execute();
  108. }
  109. /**
  110. * Respond to XML sitemap link clearing and rebuilding.
  111. *
  112. * @param array $types
  113. * An array of link types that are being rebuilt.
  114. * @param bool $save_custom
  115. * If links with overridden status and/or priority are being removed or not.
  116. */
  117. function hook_xmlsitemap_rebuild_clear(array $types, $save_custom) {
  118. db_delete('mytable')
  119. ->condition('link_type', $types, 'IN')
  120. ->execute();
  121. }
  122. /**
  123. * Index links for the XML sitemaps.
  124. */
  125. function hook_xmlsitemap_index_links($limit) {
  126. }
  127. /**
  128. * Provide information about contexts available to XML sitemap.
  129. *
  130. * @see hook_xmlsitemap_context_info_alter().
  131. */
  132. function hook_xmlsitemap_context_info() {
  133. $info['vocabulary'] = array(
  134. 'label' => t('Vocabulary'),
  135. 'summary callback' => 'mymodule_xmlsitemap_vocabulary_context_summary',
  136. 'default' => 0,
  137. );
  138. return $info;
  139. }
  140. /**
  141. * Alter XML sitemap context info.
  142. *
  143. * @see hook_xmlsitemap_context_info().
  144. */
  145. function hook_xmlsitemap_context_info_alter(&$info) {
  146. $info['vocabulary']['label'] = t('Site vocabularies');
  147. }
  148. /**
  149. * Provide information about the current context on the site.
  150. *
  151. * @see hook_xmlsitemap_context_alter()
  152. */
  153. function hook_xmlsitemap_context() {
  154. $context = array();
  155. if ($vid = mymodule_get_current_vocabulary()) {
  156. $context['vocabulary'] = $vid;
  157. }
  158. return $context;
  159. }
  160. /**
  161. * Alter the current context information.
  162. *
  163. * @see hook_xmlsitemap_context()
  164. */
  165. function hook_xmlsitemap_context_alter(&$context) {
  166. if (user_access('administer taxonomy')) {
  167. unset($context['vocabulary']);
  168. }
  169. }
  170. /**
  171. * Provide options for the url() function based on an XML sitemap context.
  172. */
  173. function hook_xmlsitemap_context_url_options(array $context) {
  174. }
  175. /**
  176. * Alter the url() options based on an XML sitemap context.
  177. */
  178. function hook_xmlsitemap_context_url_options_alter(array &$options, array $context) {
  179. }
  180. /**
  181. * Alter the content added to an XML sitemap for an individual element.
  182. *
  183. * This hooks is called when the module is generating the XML content for the
  184. * sitemap and allows other modules to alter existing or add additional XML data
  185. * for any element by adding additional key value paris to the $element array.
  186. *
  187. * The key in the element array is then used as the name of the XML child
  188. * element to add and the value is the value of that element. For example:
  189. *
  190. * @code $element['video:title'] = 'Big Ponycorn'; @endcode
  191. *
  192. * Would result in a child element like <video:title>Big Ponycorn</video:title>
  193. * being added to the sitemap for this particular link.
  194. *
  195. * @param array $element
  196. * The element that will be converted to XML for the link.
  197. * @param array $link
  198. * An array of properties providing context about the link that we are
  199. * generating an XML element for.
  200. * @param object $sitemap
  201. * The sitemap that is currently being generated.
  202. */
  203. function hook_xmlsitemap_element_alter(array &$element, array $link, $sitemap) {
  204. if ($link['subtype'] === 'video') {
  205. $node = node_load($link['id']);
  206. $element['video:video'] = array(
  207. 'video:title' => check_plain($node->title),
  208. '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']),
  209. 'video:live' => 'no',
  210. );
  211. }
  212. }
  213. /**
  214. * Alter the attributes used for the root element of the XML sitemap.
  215. *
  216. * For example add an xmlns:video attribute:
  217. * <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
  218. *
  219. * @param array $attributes
  220. * An associative array of attributes to use in the root element of an XML
  221. * sitemap.
  222. * @param object $sitemap
  223. * The sitemap that is currently being generated.
  224. */
  225. function hook_xmlsitemap_root_attributes_alter(&$attributes, $sitemap) {
  226. $attributes['xmlns:video'] = 'http://www.google.com/schemas/sitemap-video/1.1';
  227. }
  228. /**
  229. * Alter the query selecting data from {xmlsitemap} during sitemap generation.
  230. *
  231. * @param $query
  232. * A Query object describing the composite parts of a SQL query.
  233. *
  234. * @see hook_query_TAG_alter()
  235. */
  236. function hook_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
  237. $sitemap = $query->getMetaData('sitemap');
  238. if (!empty($sitemap->context['vocabulary'])) {
  239. $node_condition = db_and();
  240. $node_condition->condition('type', 'taxonomy_term');
  241. $node_condition->condition('subtype', $sitemap->context['vocabulary']);
  242. $normal_condition = db_and();
  243. $normal_condition->condition('type', 'taxonomy_term', '<>');
  244. $condition = db_or();
  245. $condition->condition($node_condition);
  246. $condition->condition($normal_condition);
  247. $query->condition($condition);
  248. }
  249. }
  250. /**
  251. * Provide information about XML sitemap bulk operations.
  252. */
  253. function hook_xmlsitemap_sitemap_operations() {
  254. }
  255. /**
  256. * Respond to XML sitemap deletion.
  257. *
  258. * This hook is invoked from xmlsitemap_sitemap_delete_multiple() after the XML
  259. * sitemap has been removed from the table in the database.
  260. *
  261. * @param $sitemap
  262. * The XML sitemap object that was deleted.
  263. */
  264. function hook_xmlsitemap_sitemap_delete(stdClass $sitemap) {
  265. db_query("DELETE FROM {mytable} WHERE smid = '%s'", $sitemap->smid);
  266. }
  267. /**
  268. * @} End of "addtogroup hooks".
  269. */