diff --git a/core/modules/menu/config/schema/menu.schema.yml b/core/modules/menu/config/schema/menu.schema.yml index e91b4d7..5431b7b 100644 --- a/core/modules/menu/config/schema/menu.schema.yml +++ b/core/modules/menu/config/schema/menu.schema.yml @@ -13,3 +13,11 @@ menu.settings: override_parent_selector: type: boolean label: 'Override parent selector' + +menu.entity.node.*: + type: mapping + label: 'Per-content type menu settings' + mapping: + available_menus: + type: array + label: 'Available menus' diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 189c479..7cbfe09 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -270,7 +270,7 @@ function menu_parent_options(array $menus, MenuLink $menu_link = NULL, $type = N } else { // If a node type is set, use all available menus for this type. - $type_menus = variable_get('menu_options_' . $type, array('main' => 'main')); + $type_menus =\Drupal::config('menu.entity.node.'.$type)->get('available_menus') ?: array('main'); foreach ($type_menus as $menu) { $available_menus[$menu] = $menu; } @@ -403,12 +403,13 @@ function menu_node_predelete(EntityInterface $node) { function menu_node_prepare_form(NodeInterface $node, $form_display, $operation, array &$form_state) { 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->getType(), 'main:0'), ':'); + $node_type_config = \Drupal::config('menu.entity.node.'.$node->getType()); + $menu_name = strtok($node_type_config->get('parent') ?: 'main:0', ':'); $menu_link = FALSE; if ($node->id()) { $mlid = FALSE; // Give priority to the default menu - $type_menus = variable_get('menu_options_' . $node->getType(), array('main' => 'main')); + $type_menus = $node_type_config->get('available_menus') ?: array('main'); if (in_array($menu_name, $type_menus)) { $query = \Drupal::entityQuery('menu_link') ->condition('link_path', 'node/' . $node->id()) @@ -526,7 +527,12 @@ 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:0')); + if ($link['mlid']) { + $default = $link['menu_name'] . ':' . $link['plid']; + } + else { + $default = \Drupal::config('menu.entity.node.'.$type)->get('parent') ?: 'main:0'; + } // 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 @@ -576,10 +582,14 @@ function menu_node_submit(EntityInterface $node, $form, $form_state) { * Implements hook_form_FORM_ID_alter(). * * Adds menu options to the node type form. + * + * @see NodeTypeFormController::form(). + * @see menu_form_node_type_form_submit(). */ function menu_form_node_type_form_alter(&$form, $form_state) { $menu_options = menu_get_menus(); $type = $form_state['controller']->getEntity(); + $node_type_config = \Drupal::config('menu.entity.node.'.$type->id()); $form['menu'] = array( '#type' => 'details', '#title' => t('Menu settings'), @@ -592,7 +602,7 @@ 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->id(), array('main')), + '#default_value' => $node_type_config->get('available_menus') ?: array('main'), '#options' => $menu_options, '#description' => t('The menus available to place links in for this content type.'), ); @@ -605,7 +615,7 @@ 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->id(), 'main:0'), + '#default_value' => $node_type_config->get('parent') ?: 'main:0', '#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')), @@ -617,6 +627,27 @@ function menu_form_node_type_form_alter(&$form, $form_state) { '(function ($) { Drupal.menuUpdateParentList(); })(jQuery);', array('scope' => 'footer', 'type' => 'inline') ); + + $form['actions']['submit']['#submit'][] = 'menu_form_node_type_form_submit'; +} + +/** + * Submit handler for forms with menu options. + * + * @see menu_form_node_type_form_alter(). + */ +function menu_form_node_type_form_submit(&$form, $form_state) { + $type = $form_state['controller']->getEntity(); + $available_menus = array(); + foreach ($form_state['values']['menu_options'] as $value) { + if (!empty($value)) { + $available_menus[] = $value; + } + } + \Drupal::config('menu.entity.node.'.$type->id()) + ->set('available_menus', $available_menus) + ->set('parent', $form_state['values']['menu_parent']) + ->save(); } /** diff --git a/core/modules/node/lib/Drupal/node/NodeTypeFormController.php b/core/modules/node/lib/Drupal/node/NodeTypeFormController.php index d9d5666..99b0a6f 100644 --- a/core/modules/node/lib/Drupal/node/NodeTypeFormController.php +++ b/core/modules/node/lib/Drupal/node/NodeTypeFormController.php @@ -197,32 +197,6 @@ public function save(array $form, array &$form_state) { // module alters the title field. $type->has_title = ($type->title_label != ''); - $variables = $form_state['values']; - - // Do not save settings from vertical tabs. - // @todo Fix vertical_tabs. - unset($variables['additional_settings__active_tab']); - - // @todo Remove the entire following code after converting node settings of - // Comment and Menu module. https://drupal.org/node/2026165 - // Remove all node type entity properties. - foreach (get_class_vars(get_class($type)) as $key => $value) { - unset($variables[$key]); - } - // Save or reset persistent variable values. - foreach ($variables as $key => $value) { - $variable_new = $key . '_' . $type->id(); - $variable_old = $key . '_' . $type->getOriginalID(); - if (is_array($value)) { - $value = array_keys(array_filter($value)); - } - variable_set($variable_new, $value); - if ($variable_new != $variable_old) { - variable_del($variable_old); - } - } - // Saving the content type after saving the variables allows modules to act - // on those variables via hook_node_type_insert(). $status = $type->save(); $t_args = array('%name' => $type->label()); diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php index 31bc82b..c607477 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php @@ -170,8 +170,10 @@ function testBreadCrumbs() { // breadcrumbs may differ, possibly due to theme overrides. $menus = array('main', 'tools'); // Alter node type menu settings. - variable_set("menu_options_$type", $menus); - variable_set("menu_parent_$type", 'tools:0'); + \Drupal::config('menu.entity.node.'.$type) + ->set('available_menus', $menus) + ->set('parent', 'tools:0') + ->save(); foreach ($menus as $menu) { // Create a parent node in the current menu.