menu.install 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the menu module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function menu_schema() {
  10. $schema['menu_custom'] = array(
  11. 'description' => 'Holds definitions for top-level custom menus (for example, Main menu).',
  12. 'fields' => array(
  13. 'menu_name' => array(
  14. 'type' => 'varchar',
  15. 'length' => 32,
  16. 'not null' => TRUE,
  17. 'default' => '',
  18. 'description' => 'Primary Key: Unique key for menu. This is used as a block delta so length is 32.',
  19. ),
  20. 'title' => array(
  21. 'type' => 'varchar',
  22. 'length' => 255,
  23. 'not null' => TRUE,
  24. 'default' => '',
  25. 'description' => 'Menu title; displayed at top of block.',
  26. 'translatable' => TRUE,
  27. ),
  28. 'description' => array(
  29. 'type' => 'text',
  30. 'not null' => FALSE,
  31. 'description' => 'Menu description.',
  32. 'translatable' => TRUE,
  33. ),
  34. ),
  35. 'primary key' => array('menu_name'),
  36. );
  37. return $schema;
  38. }
  39. /**
  40. * Implements hook_install().
  41. */
  42. function menu_install() {
  43. $system_menus = menu_list_system_menus();
  44. $t = get_t();
  45. $descriptions = array(
  46. 'navigation' => $t('The <em>Navigation</em> menu contains links intended for site visitors. Links are added to the <em>Navigation</em> menu automatically by some modules.'),
  47. 'user-menu' => $t("The <em>User</em> menu contains links related to the user's account, as well as the 'Log out' link."),
  48. 'management' => $t('The <em>Management</em> menu contains links for administrative tasks.'),
  49. 'main-menu' => $t('The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.'),
  50. );
  51. foreach ($system_menus as $menu_name => $title) {
  52. $menu = array(
  53. 'menu_name' => $menu_name,
  54. 'title' => $t($title),
  55. 'description' => $descriptions[$menu_name],
  56. );
  57. menu_save($menu);
  58. }
  59. }
  60. /**
  61. * Implements hook_uninstall().
  62. */
  63. function menu_uninstall() {
  64. menu_rebuild();
  65. }
  66. /**
  67. * @addtogroup updates-7.x-extra
  68. * @{
  69. */
  70. /**
  71. * Migrate the "Default menu for content" setting to individual node types.
  72. */
  73. function menu_update_7000() {
  74. // Act only on sites originally on Drupal 6 that have a custom "Default menu
  75. // for content" setting.
  76. $default_node_menu = variable_get('menu_default_node_menu');
  77. if (isset($default_node_menu)) {
  78. // Remove variable no longer used in Drupal 7.
  79. variable_del('menu_default_node_menu');
  80. // Make sure the menu chosen as the default still exists.
  81. $defined_menus = db_query('SELECT * FROM {menu_custom}')->fetchAllAssoc('menu_name', PDO::FETCH_ASSOC);
  82. // If the menu does not exist, do nothing; nodes will use the default D7
  83. // node menu settings.
  84. if (!isset($defined_menus[$default_node_menu])) {
  85. return;
  86. }
  87. // Update the menu settings for each node type.
  88. foreach (_update_7000_node_get_types() as $type => $type_object) {
  89. $type_menus = variable_get('menu_options_' . $type);
  90. // If the site already has a custom menu setting for this node type (set
  91. // on the initial upgrade to Drupal 7.0), don't override it.
  92. if (!isset($type_menus)) {
  93. // Set up this node type so that the Drupal 6 "Default menu for content"
  94. // is still available in the "Menu settings" section.
  95. variable_set('menu_options_' . $type, array($default_node_menu));
  96. variable_set('menu_parent_' . $type, $default_node_menu . ':0');
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Rename "Primary Links" and "Secondary Links" to their Drupal 7 equivalents.
  103. */
  104. function menu_update_7001() {
  105. // Migrate D6 menu_primary_links_source to D7 menu_main_links_source (without
  106. // renaming).
  107. if (variable_get('menu_primary_links_source') !== NULL) {
  108. variable_set('menu_main_links_source', variable_get('menu_primary_links_source'));
  109. variable_del('menu_primary_links_source');
  110. }
  111. // Rename each menu, and any settings that refer to the old menu name.
  112. // - "Primary Links" has become system menu "Main menu".
  113. // - "Secondary Links" has become a new custom menu "Secondary menu".
  114. $rename = array(
  115. 'primary-links' => array('main-menu', 'Main menu'),
  116. 'secondary-links' => array('secondary-menu', 'Secondary menu'),
  117. );
  118. foreach ($rename as $from_menu => $to) {
  119. list($to_menu, $to_title) = $to;
  120. // Rename the menu, and links in the menu.
  121. db_update('menu_custom')
  122. ->fields(array('menu_name' => $to_menu, 'title' => $to_title))
  123. ->condition('menu_name', $from_menu)
  124. ->execute();
  125. db_update('menu_links')
  126. ->fields(array('menu_name' => $to_menu))
  127. ->condition('menu_name', $from_menu)
  128. ->execute();
  129. // Update any content type that used this menu as a default menu.
  130. // Note: these variables may be unset/default, in which case we leave them
  131. // alone. See menu_update_7000()
  132. foreach (_update_7000_node_get_types() as $type => $type_object) {
  133. $menu_options = variable_get('menu_options_' . $type);
  134. if ($menu_options !== NULL) {
  135. variable_set('menu_options_' . $type, str_replace($from_menu, $to_menu, $menu_options));
  136. if (variable_get('menu_parent_' . $type) == $from_menu . ':0') {
  137. variable_set('menu_parent_' . $type, $to_menu . ':0');
  138. }
  139. }
  140. }
  141. // Update the "source for primary links" and "source for secondary links" to
  142. // follow.
  143. if (variable_get('menu_main_links_source') == $from_menu) {
  144. variable_set('menu_main_links_source', $to_menu);
  145. }
  146. if (variable_get('menu_secondary_links_source') == $from_menu) {
  147. variable_set('menu_secondary_links_source', $to_menu);
  148. }
  149. }
  150. }
  151. /**
  152. * Rename the primary/secondary menu blocks to match previously renamed menus.
  153. */
  154. function menu_update_7002(&$sandbox) {
  155. // Check for the presence of old or new table names.
  156. if (db_table_exists('blocks') || db_table_exists('block')) {
  157. $renamed_deltas = array(
  158. 'menu' => array(
  159. 'primary-links' => 'main-menu',
  160. 'secondary-links' => 'secondary-menu',
  161. ),
  162. );
  163. $moved_deltas = array(
  164. 'menu' => array('main-menu' => 'system'),
  165. );
  166. update_fix_d7_block_deltas($sandbox, $renamed_deltas, $moved_deltas);
  167. }
  168. }
  169. /**
  170. * Add missing custom menus to active menus list.
  171. */
  172. function menu_update_7003(&$sandbox) {
  173. // Make sure all custom menus are present in the active menus variable so that
  174. // their items may appear in the active trail.
  175. // @see menu_set_active_menu_names()
  176. $active_menus = variable_get('menu_default_active_menus', array_keys(menu_list_system_menus()));
  177. $update_variable = FALSE;
  178. foreach (menu_get_names() as $menu_name) {
  179. if (!in_array($menu_name, $active_menus) && (strpos($menu_name, 'menu-') === 0)) {
  180. $active_menus[] = $menu_name;
  181. $update_variable = TRUE;
  182. }
  183. }
  184. if ($update_variable) {
  185. variable_set('menu_default_active_menus', $active_menus);
  186. }
  187. // Clear the menu cache.
  188. cache_clear_all(NULL, 'cache_menu');
  189. }
  190. /**
  191. * @} End of "addtogroup updates-7.x-extra".
  192. * The next series of updates should start at 8000.
  193. */