From aba4b91ecd90209ae9964b050c6a82ceb5390a90 Mon Sep 17 00:00:00 2001 From: "Adam J. DiCarlo" Date: Sun, 22 Jul 2012 21:53:53 -0700 Subject: [PATCH] Issue #1697170 by nick_schuch, adamdicarlo: Convert menu_configure() to new configuration system. --- core/includes/menu.inc | 15 +++++++------ core/modules/menu/config/menu.yml | 3 ++ core/modules/menu/menu.admin.inc | 30 ++++++++++++++++++++++----- core/modules/menu/menu.install | 4 +++ core/modules/menu/menu.module | 40 ++++++++++++++++++++++++++---------- 5 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 core/modules/menu/config/menu.yml diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 0dcb60c..6c44ea1 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -1757,7 +1757,7 @@ function menu_list_system_menus() { * Return an array of links to be rendered as the Main menu. */ function menu_main_menu() { - return menu_navigation_links(variable_get('menu_main_links_source', 'main-menu')); + return menu_navigation_links(config('menu')->get('main_links_source')); } /** @@ -1767,11 +1767,12 @@ function menu_secondary_menu() { // If the secondary menu source is set as the primary menu, we display the // second level of the primary menu. - if (variable_get('menu_secondary_links_source', 'user-menu') == variable_get('menu_main_links_source', 'main-menu')) { - return menu_navigation_links(variable_get('menu_main_links_source', 'main-menu'), 1); + $config = config('menu'); + if ($config->get('secondary_links_source') == $config->get('main_links_source')) { + return menu_navigation_links($config->get('main_links_source'), 1); } else { - return menu_navigation_links(variable_get('menu_secondary_links_source', 'user-menu'), 0); + return menu_navigation_links($config->get('secondary_links_source'), 0); } } @@ -2243,10 +2244,10 @@ function theme_menu_local_tasks(&$variables) { * * @return * An array of menu machine names, in order of preference. The - * 'menu_default_active_menus' variable may be used to assert a menu order + * 'menu.default_active_menus' variable may be used to assert a menu order * different from the order of creation, or to prevent a particular menu from * being used at all in the active trail. - * E.g., $conf['menu_default_active_menus'] = array('navigation', 'main-menu') + * E.g., $conf['menu.default_active_menus'] = array('navigation', 'main-menu') */ function menu_set_active_menu_names($menu_names = NULL) { $active = &drupal_static(__FUNCTION__); @@ -2255,7 +2256,7 @@ function menu_set_active_menu_names($menu_names = NULL) { $active = $menu_names; } elseif (!isset($active)) { - $active = variable_get('menu_default_active_menus', array_keys(menu_list_system_menus())); + $active = config('menu')->get('default_active_menus', array_keys(menu_list_system_menus())); } return $active; } diff --git a/core/modules/menu/config/menu.yml b/core/modules/menu/config/menu.yml new file mode 100644 index 0000000..5f2464f --- /dev/null +++ b/core/modules/menu/config/menu.yml @@ -0,0 +1,3 @@ +main_links_source: main-menu +secondary_links_source: user-menu +override_parent_selector: false diff --git a/core/modules/menu/menu.admin.inc b/core/modules/menu/menu.admin.inc index 05a99e4..4ded937 100644 --- a/core/modules/menu/menu.admin.inc +++ b/core/modules/menu/menu.admin.inc @@ -664,6 +664,8 @@ function menu_reset_item_confirm_submit($form, &$form_state) { * Menu callback; Build the form presenting menu configuration options. */ function menu_configure() { + $config = config('menu'); + $form['intro'] = array( '#type' => 'item', '#markup' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. To configure these settings for a particular content type, visit the Content types page, click the edit link for the content type, and go to the Menu settings section.', array('@content-types' => url('admin/structure/types'))), @@ -671,26 +673,42 @@ function menu_configure() { $menu_options = menu_get_menus(); - $main = variable_get('menu_main_links_source', 'main-menu'); - $form['menu_main_links_source'] = array( + $main = $config->get('main_links_source'); + $form['main_links_source'] = array( '#type' => 'select', '#title' => t('Source for the Main links'), - '#default_value' => variable_get('menu_main_links_source', 'main-menu'), + '#default_value' => $config->get('main_links_source'), '#empty_option' => t('No Main links'), '#options' => $menu_options, '#tree' => FALSE, '#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'), ); - $form['menu_secondary_links_source'] = array( + $form['secondary_links_source'] = array( '#type' => 'select', '#title' => t('Source for the Secondary links'), - '#default_value' => variable_get('menu_secondary_links_source', 'user-menu'), + '#default_value' => $config->get('secondary_links_source'), '#empty_option' => t('No Secondary links'), '#options' => $menu_options, '#tree' => FALSE, '#description' => t('Select the source for the Secondary links. An advanced option allows you to use the same source for both Main links (currently %main) and Secondary links: if your source menu has two levels of hierarchy, the top level menu links will appear in the Main links, and the children of the active link will appear in the Secondary links.', array('%main' => $main ? $menu_options[$main] : t('none'))), ); - return system_settings_form($form); + return system_config_form($form, $form_state); +} + +/** + * Form submission handler for menu_configure(). + * + * @see menu_configure() + */ +function menu_configure_submit($form, &$form_state) { + $config = config('menu'); + $config_items = array('main_links_source', 'secondary_links_source'); + foreach ($config_items as $config_item) { + if ($config->get($config_item) != $form_state['values'][$config_item]) { + $config-set($config_item, $form_state['values'][$config_item]); + } + } + $config->save(); } diff --git a/core/modules/menu/menu.install b/core/modules/menu/menu.install index d876017..f3b45f5 100644 --- a/core/modules/menu/menu.install +++ b/core/modules/menu/menu.install @@ -60,6 +60,10 @@ function menu_install() { ); menu_save($menu); } + + // Set default config values. + config('menu')->set('default_active_menus', array_keys(menu_get_menus())); + config('menu')->save(); } /** diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 9015f67..543c5a8 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -274,10 +274,12 @@ function menu_save($menu) { // Make sure the menu is present in the active menus variable so that its // items may appear in the menu active trail. // @see menu_set_active_menu_names() - $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus())); + $config = config('menu'); + $active_menus = $config->get('default_active_menus'); if (!in_array($menu['menu_name'], $active_menus)) { $active_menus[] = $menu['menu_name']; - variable_set('menu_default_active_menus', $active_menus); + $config->set('default_active_menus', $active_menus); + $config->save(); } module_invoke_all('menu_insert', $menu); @@ -318,11 +320,12 @@ function menu_delete($menu) { menu_delete_links($menu['menu_name']); // Remove menu from active menus variable. - $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus())); + $active_menus = $config->get('default_active_menus'); foreach ($active_menus as $i => $menu_name) { if ($menu['menu_name'] == $menu_name) { unset($active_menus[$i]); - variable_set('menu_default_active_menus', $active_menus); + $config->set('default_active_menus', $active_menus); + $config->save(); } } @@ -358,7 +361,7 @@ function menu_parent_options($menus, $item, $type = '') { // allow contrib modules to provide more scalable pattern choosers. // hook_form_alter is too late in itself because all the possible parents are // retrieved here, unless menu_override_parent_selector is set to TRUE. - if (variable_get('menu_override_parent_selector', FALSE)) { + if (config('menu')->get('override_parent_selector')) { return array(); } @@ -375,7 +378,10 @@ function menu_parent_options($menus, $item, $type = '') { } else { // If a node type is set, use all available menus for this type. - $type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu')); + // @todo: Node module will have to handle the setting of this config value. + // $type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu')); + $type_menus = config('menu')->get('options_' . $type); + foreach ($type_menus as $menu) { $available_menus[$menu] = $menu; } @@ -568,12 +574,17 @@ function menu_node_predelete(Node $node) { function menu_node_prepare(Node $node) { if (empty($node->menu)) { // Prepare the node for the edit form so that $node->menu always exists. - $menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':'); + // @todo: Node module will have to handle the setting of this config value. + //$menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':'); + $config = config('menu'); + $menu_name = strtok($config->get('parent_' . $node->type), ':'); $item = array(); if (isset($node->nid)) { $mlid = FALSE; // Give priority to the default menu - $type_menus = variable_get('menu_options_' . $node->type, array('main-menu' => 'main-menu')); + // @todo: Node module will have to handle the setting of this config value. + // $type_menus = variable_get('menu_options_' . $node->type, array('main-menu' => 'main-menu')); + $type_menus = $config->get('options_' . $node->type); if (in_array($menu_name, $type_menus)) { $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array( ':path' => 'node/' . $node->nid, @@ -688,7 +699,9 @@ function menu_form_node_form_alter(&$form, $form_state) { '#description' => t('Shown when hovering over the menu link.'), ); - $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0')); + // @todo: Node module will have to handle the setting of this config value. + // $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0')); + $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : config('menu')->get('parent_' . $type)); // If the current parent menu item is not present in options, use the first // available option as default value. // @todo User should not be allowed to access menu link settings in such a @@ -732,6 +745,7 @@ function menu_node_submit(Node $node, $form, $form_state) { * Adds menu options to the node type form. */ function menu_form_node_type_form_alter(&$form, $form_state) { + $config = config('menu'); $menu_options = menu_get_menus(); $type = $form['#node_type']; $form['menu'] = array( @@ -747,7 +761,9 @@ function menu_form_node_type_form_alter(&$form, $form_state) { $form['menu']['menu_options'] = array( '#type' => 'checkboxes', '#title' => t('Available menus'), - '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')), + // @todo: Node module will have to handle the setting of this config value. + // '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')), + '#default_value' => $config->get('menu_options_' . $type->type), '#options' => $menu_options, '#description' => t('The menus available to place links in for this content type.'), ); @@ -759,7 +775,9 @@ function menu_form_node_type_form_alter(&$form, $form_state) { $form['menu']['menu_parent'] = array( '#type' => 'select', '#title' => t('Default parent item'), - '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'), + // @todo: Node module will have to handle the setting of this config value. + // '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'), + '#default_value' => $config->get('menu_parent_' . $type->type), '#options' => $options, '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'), '#attributes' => array('class' => array('menu-title-select')), -- 1.7.8