xmlsitemap_menu.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Implements hook_entity_info_alter().
  4. *
  5. * Adds support for the menu link entity if it doesn't already exist.
  6. */
  7. function xmlsitemap_menu_entity_info_alter(&$info) {
  8. if (!isset($info['menu_link'])) {
  9. $info['menu_link'] = array(
  10. 'label' => t('Menu link'),
  11. 'controller class' => 'DrupalDefaultEntityController',
  12. 'base table' => 'menu_links',
  13. 'uri callback' => 'xmlsitemap_menu_menu_link_uri',
  14. 'fieldable' => FALSE,
  15. 'static cache' => TRUE,
  16. 'field cache' => TRUE,
  17. 'entity keys' => array(
  18. 'id' => 'mlid',
  19. 'bundle' => 'menu_name',
  20. 'label' => 'link_title',
  21. 'revision' => '',
  22. ),
  23. 'load hook' => NULL,
  24. 'view modes' => array(),
  25. 'translation' => array(),
  26. 'schema_fields_sql' => array(
  27. 'base table' => drupal_schema_fields_sql('menu_links'),
  28. ),
  29. 'xmlsitemap' => array(
  30. 'process callback' => 'xmlsitemap_menu_xmlsitemap_process_menu_links',
  31. ),
  32. 'bundle label' => t('Menu'),
  33. );
  34. foreach (menu_get_menus() as $type => $name) {
  35. $info['menu_link']['bundles'][$type] = array(
  36. 'label' => $name,
  37. 'admin' => array(
  38. 'path' => 'admin/structure/menu/manage/%menu/edit',
  39. 'bundle argument' => 4,
  40. 'real path' => 'admin/structure/menu/manage/' . $type . '/edit',
  41. 'access arguments' => array('administer menus'),
  42. ),
  43. );
  44. }
  45. }
  46. }
  47. /**
  48. * Entity URI callback.
  49. */
  50. function xmlsitemap_menu_menu_link_uri($menu_item) {
  51. return is_array($menu_item) ? $menu_item['href'] : $menu_item->href;
  52. }
  53. /**
  54. * Implements hook_cron().
  55. *
  56. * Process old menu links not found in the {xmlsitemap} table.
  57. */
  58. function xmlsitemap_menu_cron() {
  59. xmlsitemap_menu_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
  60. }
  61. /**
  62. * Implements hook_xmlsitemap_index_links().
  63. */
  64. function xmlsitemap_menu_xmlsitemap_index_links($limit) {
  65. if ($menus = xmlsitemap_get_link_type_enabled_bundles('menu_link')) {
  66. $sql = "SELECT ml.mlid FROM {menu_links} ml LEFT JOIN {xmlsitemap} x ON x.type = 'menu' AND ml.mlid = x.id WHERE x.id IS NULL AND ml.menu_name IN (:menus) ORDER BY ml.mlid DESC";
  67. $mlids = db_query_range($sql, 0, $limit, array(':menus' => $menus))->fetchCol();
  68. xmlsitemap_menu_xmlsitemap_process_menu_links($mlids);
  69. }
  70. }
  71. /**
  72. * Process menu sitemap links.
  73. *
  74. * @param $mlids
  75. * An array of menu link IDs.
  76. */
  77. function xmlsitemap_menu_xmlsitemap_process_menu_links(array $mlids, array $xmlsitemap = array()) {
  78. // Set the global user variable to the anonymous user.
  79. xmlsitemap_switch_user(0);
  80. foreach ($mlids as $mlid) {
  81. $menu_item = menu_link_load($mlid);
  82. if (empty($menu_item)) {
  83. continue;
  84. }
  85. if (!empty($xmlsitemap)) {
  86. $menu_item['xmlsitemap'] = $xmlsitemap;
  87. }
  88. $link = xmlsitemap_menu_create_link($menu_item);
  89. xmlsitemap_link_save($link);
  90. }
  91. // Set the global user variable back to the original user.
  92. xmlsitemap_restore_user();
  93. }
  94. /**
  95. * Implements hook_form_FORM_ID_alter().
  96. *
  97. * @see menu_edit_menu()
  98. * @see xmlsitemap_add_link_bundle_settings()
  99. */
  100. function xmlsitemap_menu_form_menu_edit_menu_alter(&$form, $form_state) {
  101. $menu = isset($form['menu_name']['#default_value']) ? $form['menu_name']['#default_value'] : '';
  102. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  103. xmlsitemap_add_link_bundle_settings($form, $form_state, 'menu_link', $menu);
  104. }
  105. //function xmlsitemap_menu_form_menu_overview_form_alter(&$form, $form_state) {
  106. // $form['#submit'][] = 'xmlsitemap_menu_menu_overview_form_submit';
  107. //}
  108. //
  109. //function xmlsitemap_menu_menu_overview_form_submit($form, $form_state) {
  110. // $mlids = array();
  111. // foreach (element_children($form) as $mlid) {
  112. // if (isset($form[$mlid]['#item'])) {
  113. // $mlids[] = $form[$mlid]['#item']['mlid'];
  114. // }
  115. // }
  116. // xmlsitemap_menu_xmlsitemap_process_menu_links($mlids);
  117. //}
  118. /**
  119. * Implements hook_form_FORM_ID_alter().
  120. *
  121. * @see menu_edit_item()
  122. */
  123. function xmlsitemap_menu_form_menu_edit_item_alter(&$form, $form_state) {
  124. $menu_name = $form['parent']['#default_value'];
  125. $menu_name = substr($menu_name, 0, strpos($menu_name, ':'));
  126. // Add the link options.
  127. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  128. xmlsitemap_add_form_link_options($form, 'menu_link', $menu_name, $form['mlid']['#value']);
  129. $form['xmlsitemap']['#weight'] = 30;
  130. }
  131. /**
  132. * Implements hook_menu_insert().
  133. */
  134. function xmlsitemap_menu_menu_insert(array $menu) {
  135. if (isset($menu['xmlsitemap'])) {
  136. xmlsitemap_link_bundle_settings_save('menu_link', $menu['menu_name'], $menu['xmlsitemap']);
  137. }
  138. // When menus change, the bundles we defined in
  139. // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  140. entity_info_cache_clear();
  141. }
  142. /**
  143. * Implements hook_menu_update().
  144. */
  145. function xmlsitemap_menu_menu_update(array $menu) {
  146. if (isset($menu['xmlsitemap'])) {
  147. xmlsitemap_link_bundle_settings_save('menu_link', $menu['menu_name'], $menu['xmlsitemap']);
  148. }
  149. // When menus change, the bundles we defined in
  150. // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  151. entity_info_cache_clear();
  152. }
  153. /**
  154. * Implements hook_menu_delete().
  155. */
  156. function xmlsitemap_menu_menu_delete(array $menu) {
  157. xmlsitemap_link_bundle_delete('menu_link', $menu['menu_name']);
  158. // When menus change, the bundles we defined in
  159. // xmlsitemap_menu_entity_info_alter() change, so we need to clear the cache.
  160. entity_info_cache_clear();
  161. }
  162. /**
  163. * Implements hook_menu_link_insert().
  164. */
  165. function xmlsitemap_menu_menu_link_insert(array $link) {
  166. $link += array('xmlsitemap' => array());
  167. xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
  168. }
  169. /**
  170. * Implements hook_menu_link_update().
  171. *
  172. * @see hook_menu_link_alter()
  173. */
  174. function xmlsitemap_menu_menu_link_update(array $link) {
  175. //$link += array('xmlsitemap' => array());
  176. //xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
  177. }
  178. /**
  179. * Implements hook_menu_link_alter().
  180. *
  181. * We have to use this hook rather than hook_menu_link_update() because this
  182. * hook is not always called if the user does not edit the core menu item
  183. * fields.
  184. *
  185. * @see http://drupal.org/node/1013856
  186. */
  187. function xmlsitemap_menu_menu_link_alter(array &$link) {
  188. if (!empty($link['mlid'])) {
  189. $link += array('xmlsitemap' => array());
  190. xmlsitemap_menu_xmlsitemap_process_menu_links(array($link['mlid']), $link['xmlsitemap']);
  191. }
  192. }
  193. /**
  194. * Implements hook_menu_link_delete().
  195. */
  196. function xmlsitemap_menu_menu_link_delete(array $link) {
  197. xmlsitemap_link_delete('menu_link', $link['mlid']);
  198. }
  199. /**
  200. * Create a sitemap link from a menu item.
  201. *
  202. * @param $menu_item
  203. * A loaded menu item.
  204. */
  205. function xmlsitemap_menu_create_link(array $menu_item) {
  206. if (!isset($menu_item['xmlsitemap'])) {
  207. $menu_item['xmlsitemap'] = array();
  208. if ($menu_item['mlid'] && $link = xmlsitemap_link_load('menu_link', $menu_item['mlid'])) {
  209. $menu_item['xmlsitemap'] = $link;
  210. }
  211. }
  212. $settings = xmlsitemap_link_bundle_load('menu_link', $menu_item['menu_name']);
  213. $menu_item['xmlsitemap'] += array(
  214. 'type' => 'menu_link',
  215. 'id' => $menu_item['mlid'],
  216. 'status' => $settings['status'],
  217. 'status_default' => $settings['status'],
  218. 'status_override' => 0,
  219. 'priority' => $settings['priority'],
  220. 'priority_default' => $settings['priority'],
  221. 'priority_override' => 0,
  222. );
  223. // The following values must always be checked because they are volatile.
  224. $menu_item['xmlsitemap']['loc'] = $menu_item['href'];
  225. $menu_item['xmlsitemap']['subtype'] = $menu_item['menu_name'];
  226. $menu_item['xmlsitemap']['access'] = $menu_item['access'] && !$menu_item['external'] && !$menu_item['hidden'];
  227. $menu_item['xmlsitemap']['language'] = isset($menu_item['options']['langcode']) ? $menu_item['options']['langcode'] : LANGUAGE_NONE;
  228. return $menu_item['xmlsitemap'];
  229. }
  230. /**
  231. * Calculate the priority of a menu link based on depth and weight.
  232. */
  233. function xmlsitemap_menu_calculate_priority(array $menu_item) {
  234. $priority = (MENU_MAX_DEPTH - $menu_item['depth'] + 1) / MENU_MAX_DEPTH;
  235. $priority -= (50 + $menu_item['weight']) / (100 * (MENU_MAX_DEPTH + 1));
  236. return $priority;
  237. }
  238. /**
  239. * Internal default variables for template_var().
  240. */
  241. function xmlsitemap_menu_variables() {
  242. $defaults = array();
  243. $menus = array_keys(menu_get_menus());
  244. foreach ($menus as $menu) {
  245. $defaults['xmlsitemap_settings_menu_' . $menu] = array(
  246. 'status' => XMLSITEMAP_STATUS_DEFAULT,
  247. 'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
  248. );
  249. }
  250. return $defaults;
  251. }