From 3129cc0548d9c4cdc59cc4061e1e1c840a0155c9 Thu, 19 Jan 2012 15:32:45 +0100
From: Bram Goffings <bramgoffings@gmail.com>
Date: Thu, 19 Jan 2012 15:32:38 +0100
Subject: [PATCH] menu ux improvement


diff --git a/core/modules/menu/menu.admin.inc b/core/modules/menu/menu.admin.inc
index f933feb..e82ebda 100644
--- a/core/modules/menu/menu.admin.inc
+++ b/core/modules/menu/menu.admin.inc
@@ -466,6 +466,26 @@
     '#title' => t('Description'),
     '#default_value' => $menu['description'],
   );
+
+  $node_types = node_type_get_names();
+  $default_value = array();
+  if ($type == 'edit') {
+    // When editing a menu we need to collect all the node types that have this
+    // menu enabled.
+    foreach ($node_types as $node_type => $name) {
+      $menu_options = variable_get('menu_options_' . $node_type, array('main-menu'));
+      if (in_array($menu['menu_name'], $menu_options)) {
+        $default_value[] = $node_type;
+      }
+    }
+  }
+  $form['menu_node_types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Make this menu available to the following content types:'),
+    '#default_value' => $default_value,
+    '#options' => $node_types,
+  );
+
   $form['actions'] = array('#type' => 'actions');
   $form['actions']['submit'] = array(
     '#type' => 'submit',
@@ -595,6 +615,22 @@
       menu_link_save($link);
     }
   }
+
+  // Check if we need to add or remove this menu from each available node type.
+  $node_types = $menu['menu_node_types'];
+  foreach ($node_types as $node_type => $value) {
+    $menu_options = variable_get('menu_options_' . $node_type, array('main-menu'));
+    if (empty($value)) {
+      // Remove this menu from the content type.
+      $menu_options = array_diff($menu_options, array($menu['menu_name']));
+    }
+    elseif (!empty($value) && !in_array($menu['menu_name'], $menu_options)) {
+      // Add this menu to the content type.
+      $menu_options[] = $menu['menu_name'];
+    }
+    variable_set('menu_options_' . $node_type, $menu_options);
+  }
+
   drupal_set_message(t('Your configuration has been saved.'));
   $form_state['redirect'] = $path . $menu['menu_name'];
 }
diff --git a/core/modules/menu/menu.test b/core/modules/menu/menu.test
index 5fa27c7..07fa565 100644
--- a/core/modules/menu/menu.test
+++ b/core/modules/menu/menu.test
@@ -131,10 +131,12 @@
     $this->drupalGet('admin/structure/menu/add');
     $menu_name = substr(hash('sha256', $this->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI + 1);
     $title = $this->randomName(16);
+    $type = 'article';
     $edit = array(
       'menu_name' => $menu_name,
       'description' => '',
       'title' =>  $title,
+      "menu_node_types[$type]" => TRUE,
     );
     $this->drupalPost('admin/structure/menu/add', $edit, t('Save'));

@@ -157,12 +159,23 @@
       '%length' => drupal_strlen($menu_name),
     )));
     // Unlike most other modules, there is no confirmation message displayed.
-
     $this->drupalGet('admin/structure/menu');
     $this->assertText($title, 'Menu created');

-    // Enable the custom menu block.
     $menu_name = 'menu-' . $menu_name; // Drupal prepends the name with 'menu-'.
+
+    // Verify this menu is listed on the article administration form.
+    $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
+    $this->assertFieldChecked('edit-menu-node-types-' . $type, t('Custom menu added to article node type'));
+
+    // Disable the menu from the article administration page.
+    $edit = array();
+    $edit['menu_node_types[' . $type . ']'] = FALSE;
+    $this->drupalPost(NULL, $edit, t('Save'));
+    $this->drupalGet('admin/structure/menu/manage/' . $menu_name . '/edit');
+    $this->assertNoFieldChecked('edit-menu-node-types-' . $type , t('Custom menu removed from article node type'));
+
+    // Enable the custom menu block.
     $edit = array();
     $edit['blocks[menu_' . $menu_name . '][region]'] = 'sidebar_first';
     $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
