xmlsitemap_menu.module 9.1 KB

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