menu_block.install 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @file
  4. * Provides install, upgrade and un-install functions for menu_block.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function menu_block_uninstall() {
  10. // Delete menu block variables.
  11. foreach (variable_get('menu_block_ids', array()) AS $delta) {
  12. variable_del("menu_block_{$delta}_title_link");
  13. variable_del("menu_block_{$delta}_admin_title");
  14. variable_del("menu_block_{$delta}_parent");
  15. variable_del("menu_block_{$delta}_level");
  16. variable_del("menu_block_{$delta}_follow");
  17. variable_del("menu_block_{$delta}_depth");
  18. variable_del("menu_block_{$delta}_expanded");
  19. variable_del("menu_block_{$delta}_sort");
  20. }
  21. variable_del('menu_block_ids');
  22. variable_del('menu_block_suppress_core');
  23. variable_del('menu_block_menu_order');
  24. // Remove block configurations.
  25. db_delete('block')
  26. ->condition('module', 'menu_block')
  27. ->execute();
  28. db_delete('block_role')
  29. ->condition('module', 'menu_block')
  30. ->execute();
  31. cache_clear_all();
  32. }
  33. /**
  34. * Implements hook_enable().
  35. */
  36. function menu_block_enable() {
  37. drupal_set_message(t('To use menu blocks, find the "Add menu block" link on the <a href="@url">administer blocks page</a>.', array('@url' => url('admin/structure/block'))));
  38. }
  39. /**
  40. * Implements hook_install().
  41. */
  42. function menu_block_install() {
  43. // No-op.
  44. }
  45. /**
  46. * Converts pre-5.x-1.0 block names to the new format.
  47. */
  48. function menu_block_update_5100() {
  49. $delta = 0;
  50. $enabled_blocks = array();
  51. // Find the old enabled blocks.
  52. foreach (variable_get('menu_block_enabled_blocks', array()) AS $old_delta => $enabled) {
  53. list($mid, $level) = explode('-', $old_delta);
  54. if ($enabled) {
  55. $enabled_blocks[++$delta] = TRUE;
  56. variable_set("menu_block_{$delta}_mid", $mid);
  57. variable_set("menu_block_{$delta}_level", $level);
  58. variable_set("menu_block_{$delta}_depth", variable_get("menu_block_depth_{$mid}_{$level}", 0));
  59. variable_set("menu_block_{$delta}_expanded", variable_get("menu_block_expanded_{$mid}_{$level}", 0));
  60. }
  61. // Remove any of the old-style variables.
  62. variable_del("menu_block_depth_{$mid}_{$level}");
  63. variable_del("menu_block_expanded_{$mid}_{$level}");
  64. }
  65. variable_set('menu_block_enabled_blocks', $enabled_blocks);
  66. return t('A pre-release version of Menu block has been detected. All menu blocks from the pre-release version have been given a new delta and are no longer placed in any block regions; their block placement should be re-configured immediately.');
  67. }
  68. /**
  69. * Converts menu block-tracking data to new format.
  70. */
  71. function menu_block_update_5200() {
  72. $block_ids = array();
  73. foreach (variable_get('menu_block_enabled_blocks', array()) AS $delta => $enabled) {
  74. if ($enabled) {
  75. $block_ids[] = $delta; // Build new $menu_block_ids.
  76. // Convert $menu_block_DELTA_mid to $menu_block_DELTA_menu_name.
  77. $mid = variable_get("menu_block_{$delta}_mid", 1);
  78. variable_set("menu_block_{$delta}_menu_name", $mid);
  79. // If we weren't upgraded to 5.x-2.x before the Drupal 6 upgrade, the
  80. // mid-to-menu_name conversion is not possible.
  81. variable_set("menu_block_{$delta}_title", $mid == 2 ? 'primary-links' : 'navigation');
  82. variable_del("menu_block_{$delta}_mid");
  83. }
  84. else {
  85. // Delete un-enabled menu block.
  86. variable_del("menu_block_{$delta}_mid");
  87. variable_del("menu_block_{$delta}_level");
  88. variable_del("menu_block_{$delta}_depth");
  89. variable_del("menu_block_{$delta}_expanded");
  90. db_delete('block')
  91. ->condition('module', 'menu_block')
  92. ->condition('delta', $delta)
  93. ->execute();
  94. db_delete('block_role')
  95. ->condition('module', 'menu_block')
  96. ->condition('delta', $delta)
  97. ->execute();
  98. }
  99. }
  100. // Finish conversion of $menu_block_enabled_blocks to $menu_block_ids.
  101. sort($block_ids);
  102. variable_set('menu_block_ids', $block_ids);
  103. variable_del('menu_block_enabled_blocks');
  104. cache_clear_all();
  105. return t('A 5.x-1.x version of Menu block has been detected and an attempt was made to upgrade it. Unfortunately, you should have upgraded to Menu block 5.x-2.x before your upgrade to Drupal 7. You may need to re-configure all your menu blocks.');
  106. }
  107. /**
  108. * Converts to Drupal 6 menu names.
  109. */
  110. function menu_block_update_6200() {
  111. $menus = menu_get_menus();
  112. foreach (variable_get('menu_block_ids', array()) AS $delta) {
  113. // Drupal 6 uses the menu title to create the new menu_name.
  114. $menu_name = preg_replace('/[^a-zA-Z0-9]/', '-', strtolower(variable_get("menu_block_{$delta}_title", '')));
  115. // If we can't find the new menu_name, default to the navigation menu.
  116. if (empty($menus[$menu_name])) {
  117. $menu_name = 'navigation';
  118. }
  119. variable_set("menu_block_{$delta}_menu_name", $menu_name);
  120. variable_del("menu_block_{$delta}_title");
  121. }
  122. return t('The 5.x-2.x version of Menu block has been upgraded to the 6.x-2.0 data storage format.');
  123. }
  124. /**
  125. * Converts the menu names to parent items.
  126. */
  127. function menu_block_update_6201() {
  128. $menus = menu_get_menus();
  129. foreach (variable_get('menu_block_ids', array()) AS $delta) {
  130. variable_set("menu_block_{$delta}_parent", variable_get("menu_block_{$delta}_menu_name", 'navigation') . ':0');
  131. variable_del("menu_block_{$delta}_menu_name");
  132. }
  133. return t('The 6.x-2.0 version of Menu block has been upgraded to the 6.x-2.1 data storage format.');
  134. }
  135. /**
  136. * Converts to Drupal 7 menu names.
  137. */
  138. function menu_block_update_7200() {
  139. foreach (variable_get('menu_block_ids', array()) AS $delta) {
  140. $menu_name = '';
  141. list($old_menu_name, $parent_mlid) = explode(':', variable_get("menu_block_{$delta}_parent", ' : '));
  142. switch ($old_menu_name) {
  143. case 'primary-links':
  144. $menu_name = 'main-menu';
  145. break;
  146. case 'secondary-links':
  147. $menu_name = 'secondary-menu';
  148. break;
  149. case 'navigation':
  150. $menu_name = 'management';
  151. break;
  152. }
  153. if ($menu_name) {
  154. variable_set("menu_block_{$delta}_parent", $menu_name . ':' . $parent_mlid);
  155. }
  156. }
  157. return t('The 6.x-2.x version of Menu block has been upgraded to use the new menu names in Drupal 7. To use menu blocks in Drupal 7, find the "Add menu block" link on the <a href="@url">administer blocks page</a>.', array('@url' => url('admin/structure/block')));
  158. }
  159. /**
  160. * Helper function to fix custom menus in Drupal 7.0.
  161. */
  162. function menu_block_fix_custom_menus() {
  163. // Make sure all custom menus are present in the active menus variable so that
  164. // their items may appear in the active trail.
  165. // @see menu_set_active_menu_names()
  166. $active_menus = variable_get('menu_default_active_menus', array_keys(menu_list_system_menus()));
  167. $update_variable = FALSE;
  168. foreach (menu_get_names() as $menu_name) {
  169. if (!in_array($menu_name, $active_menus) && (strpos($menu_name, 'menu-') === 0)) {
  170. $active_menus[] = $menu_name;
  171. $update_variable = TRUE;
  172. }
  173. }
  174. if ($update_variable) {
  175. variable_set('menu_default_active_menus', $active_menus);
  176. }
  177. // Clear the menu cache.
  178. cache_clear_all(NULL, 'cache_menu');
  179. }
  180. /**
  181. * Add missing custom menus to active menus list.
  182. */
  183. function menu_block_update_7202() {
  184. menu_block_fix_custom_menus();
  185. }