From b45baa5f1799232b958350a702fde6e8c55d417e Mon Sep 17 00:00:00 2001
From: sun <sun@unleashedmind.com>
Date: Mon, 28 Jan 2013 15:23:47 +0100
Subject: [PATCH] - #293768 by sun: Allow to enable/disable menu additions.

---
 admin_menu.admin.js                          | 14 ++++++
 admin_menu.inc                               | 66 +++++++++++++++++++++++-----
 admin_menu.install                           |  1 +
 admin_menu.module                            | 52 ++++++++++++++++++----
 admin_menu_toolbar/admin_menu_toolbar.module | 20 ++++++---
 5 files changed, 126 insertions(+), 27 deletions(-)

diff --git a/admin_menu.admin.js b/admin_menu.admin.js
index 6adf3a8..9ee9f36 100644
--- a/admin_menu.admin.js
+++ b/admin_menu.admin.js
@@ -1,6 +1,20 @@
 (function($) {
 
 /**
+ * Live preview of Administration menu components.
+ */
+Drupal.behaviors.adminMenuLivePreview = {
+  attach: function (context, settings) {
+    $('input[name^="admin_menu_components"]', context).once('admin-menu-live-preview')
+      .change(function () {
+        var target = $(this).attr('rel');
+        $(target).toggle(this.checked);
+      })
+      .trigger('change');
+  }
+};
+
+/**
  * Automatically enables required permissions on demand.
  *
  * Many users do not understand that two permissions are required for the
diff --git a/admin_menu.inc b/admin_menu.inc
index 79fefd9..682bada 100644
--- a/admin_menu.inc
+++ b/admin_menu.inc
@@ -425,6 +425,7 @@ function admin_menu_links_icon() {
 
   $links = array(
     '#theme' => 'admin_menu_links',
+    '#wrapper_attributes' => array('id' => 'admin-menu-icon'),
     '#weight' => -100,
   );
   $links['icon'] = array(
@@ -526,23 +527,16 @@ function admin_menu_links_icon() {
 }
 
 /**
- * Build user/action links; mostly account information and links.
+ * Builds the account links.
  *
  * @see theme_admin_menu_links()
  */
-function admin_menu_links_user() {
+function admin_menu_links_account() {
   $links = array(
     '#theme' => 'admin_menu_links',
+    '#wrapper_attributes' => array('id' => 'admin-menu-account'),
     '#weight' => 100,
   );
-  // Add link to show current authenticated/anonymous users.
-  $links['user-counter'] = array(
-    '#title' => admin_menu_get_user_count(),
-    '#description' => t('Current anonymous / authenticated users'),
-    '#weight' => -90,
-    '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-users')),
-    '#href' => (user_access('administer users') ? 'admin/people/people' : 'user'),
-  );
   $links['account'] = array(
     '#title' => $GLOBALS['user']->name,
     '#weight' => -99,
@@ -555,7 +549,6 @@ function admin_menu_links_user() {
     '#attributes' => array('class' => array('admin-menu-action')),
     '#href' => 'user/logout',
   );
-
   // Add Devel module switch user links.
   $switch_links = module_invoke('devel', 'switch_user_list');
   if (!empty($switch_links) && count($switch_links) > 1) {
@@ -571,7 +564,28 @@ function admin_menu_links_user() {
       );
     }
   }
+  return $links;
+}
 
+/**
+ * Builds user counter.
+ *
+ * @see theme_admin_menu_links()
+ */
+function admin_menu_links_users() {
+  $links = array(
+    '#theme' => 'admin_menu_links',
+    '#wrapper_attributes' => array('id' => 'admin-menu-users'),
+    '#weight' => 150,
+  );
+  // Add link to show current authenticated/anonymous users.
+  $links['user-counter'] = array(
+    '#title' => admin_menu_get_user_count(),
+    '#description' => t('Current anonymous / authenticated users'),
+    '#weight' => -90,
+    '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-users')),
+    '#href' => (user_access('administer users') ? 'admin/people/people' : 'user'),
+  );
   return $links;
 }
 
@@ -583,7 +597,8 @@ function admin_menu_links_user() {
 function admin_menu_links_search() {
   $links = array(
     '#theme' => 'admin_menu_links',
-    '#weight' => 150,
+    '#wrapper_attributes' => array('id' => 'admin-menu-search'),
+    '#weight' => 200,
   );
   $links['search'] = array(
     '#type' => 'textfield',
@@ -621,6 +636,33 @@ function admin_menu_theme_settings() {
     '#title' => t('Advanced settings'),
   );
 
+  $form['plugins'] = array(
+    '#type' => 'details',
+    '#title' => t('Plugins'),
+    '#group' => 'advanced',
+  );
+  $form['plugins']['admin_menu_components'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Enabled components'),
+    '#options' => array(
+      'admin_menu.icon' => t('Icon menu'),
+      'admin_menu.menu' => t('Administration menu'),
+      'admin_menu.search' => t('Search bar'),
+      'admin_menu.users' => t('User counts'),
+      'admin_menu.account' => t('Account links'),
+    ),
+  );
+  $form['plugins']['admin_menu_components']['#default_value'] = variable_get('admin_menu_components', array_keys($form['plugins']['admin_menu_components']['#options']));
+
+  // Assign 'rel' attributes to all options to achieve a live preview.
+  // Unfortunately, #states relies on wrapping .form-wrapper classes, so it
+  // cannot be used here.
+  $form['#attached']['js'][] = drupal_get_path('module', 'admin_menu') . '/admin_menu.admin.js';
+  foreach ($form['plugins']['admin_menu_components']['#options'] as $key => $label) {
+    $id = preg_replace('/[^a-z]/', '-', $key);
+    $form['plugins']['admin_menu_components'][$key]['#attributes']['rel'] = '#' . $id;
+  }
+
   $form['tweaks'] = array(
     '#type' => 'fieldset',
     '#title' => t('System tweaks'),
diff --git a/admin_menu.install b/admin_menu.install
index fa6c429..70e31c6 100644
--- a/admin_menu.install
+++ b/admin_menu.install
@@ -32,6 +32,7 @@ function admin_menu_install() {
  */
 function admin_menu_uninstall() {
   // Delete variables.
+  variable_del('admin_menu_components');
   variable_del('admin_menu_devel_modules');
   variable_del('admin_menu_devel_modules_enabled');
   variable_del('admin_menu_devel_modules_skip');
diff --git a/admin_menu.module b/admin_menu.module
index 301b4d0..048e277 100644
--- a/admin_menu.module
+++ b/admin_menu.module
@@ -444,6 +444,12 @@ function admin_menu_output() {
   $cache_server_enabled = variable_get('admin_menu_cache_server', TRUE);
   $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;
 
+  // Determine whether we need to show all components and disable all caches.
+  // @todo D8: Implement a proper plugin system to make this property obsolete.
+  if ($is_admin = current_path() == 'admin/config/administration/admin_menu' && $_SERVER['REQUEST_METHOD'] == 'GET') {
+    $cache_server_enabled = FALSE;
+  }
+
   // Try to load and output administration menu from server-side cache.
   // @todo Duplicates the page cache? Page cache ID contains the hash that is
   //   generated at the bottom of this function, which is based on $content,
@@ -461,6 +467,17 @@ function admin_menu_output() {
 
   // Rebuild the output.
   if (!isset($content)) {
+    // Retrieve enabled components to display and make them available for others.
+    $components = drupal_map_assoc(variable_get('admin_menu_components', array(
+      'admin_menu.icon',
+      'admin_menu.menu',
+      'admin_menu.account',
+    )));
+    $content['#components'] = $components;
+    if ($is_admin) {
+      $content['#is_admin'] = TRUE;
+    }
+
     // Add site name as CSS class for development/staging theming purposes. We
     // leverage the cookie domain instead of HTTP_HOST to account for many (but
     // not all) multi-domain setups (e.g. language-based sub-domains).
@@ -479,15 +496,27 @@ function admin_menu_output() {
     module_load_include('inc', 'admin_menu');
 
     // Add administration menu.
-    $content['menu'] = admin_menu_links_menu(admin_menu_tree('management'));
-    $content['menu']['#theme'] = 'admin_menu_links';
-    // Ensure the menu tree is rendered between the icon and user links.
-    $content['menu']['#weight'] = 0;
+    if (isset($components['admin_menu.menu']) || $is_admin) {
+      $content['menu'] = admin_menu_links_menu(admin_menu_tree('management'));
+      $content['menu']['#theme'] = 'admin_menu_links';
+      $content['menu']['#wrapper_attributes']['id'] = 'admin-menu-menu';
+      // Ensure the menu tree is rendered between the icon and user links.
+      $content['menu']['#weight'] = 0;
+    }
 
     // Add menu additions.
-    $content['icon'] = admin_menu_links_icon();
-    $content['user'] = admin_menu_links_user();
-    $content['search'] = admin_menu_links_search();
+    if (isset($components['admin_menu.icon']) || $is_admin) {
+      $content['icon'] = admin_menu_links_icon();
+    }
+    if (isset($components['admin_menu.account']) || $is_admin) {
+      $content['account'] = admin_menu_links_account();
+    }
+    if (isset($components['admin_menu.users']) || $is_admin) {
+      $content['users'] = admin_menu_links_users();
+    }
+    if (isset($components['admin_menu.search']) || $is_admin) {
+      $content['search'] = admin_menu_links_search();
+    }
 
     // Allow modules to enhance the menu.
     // Uses '_output' suffix for consistency with the alter hook (see below).
@@ -521,6 +550,10 @@ function admin_menu_output() {
  * Implements hook_admin_menu_output_build().
  */
 function admin_menu_admin_menu_output_build(&$content) {
+  if (!isset($content['menu'])) {
+    return;
+  }
+
   // Unassign weights for categories below Configuration.
   // An alphabetical order is more natural for a dropdown menu.
   if (isset($content['menu']['admin/config'])) {
@@ -661,7 +694,9 @@ function theme_admin_menu_links($variables) {
   // @todo #attributes probably required for UL, but already used for LI.
   // @todo Use $element['#children'] here instead.
   if ($output) {
-    $output = "\n" . '<ul class="dropdown">' . $output . '</ul>';
+    $elements['#wrapper_attributes']['class'][] = 'dropdown';
+    $attributes = drupal_attributes($elements['#wrapper_attributes']);
+    $output = "\n" . '<ul' . $attributes . '>' . $output . '</ul>';
   }
   return $output;
 }
@@ -748,6 +783,7 @@ function admin_menu_flush_caches($uid = NULL) {
  */
 function admin_menu_form_alter(&$form, &$form_state, $form_id) {
   $global_flush_ids = array(
+    'admin_menu_theme_settings' => 1,
     // Update links for clean/non-clean URLs.
     'system_clean_url_settings' => 1,
     // Incorporate changed user permissions.
diff --git a/admin_menu_toolbar/admin_menu_toolbar.module b/admin_menu_toolbar/admin_menu_toolbar.module
index 1885fce..6bec595 100644
--- a/admin_menu_toolbar/admin_menu_toolbar.module
+++ b/admin_menu_toolbar/admin_menu_toolbar.module
@@ -82,17 +82,23 @@ function admin_menu_toolbar_admin_menu_output_build(&$content) {
  */
 function admin_menu_toolbar_admin_menu_output_alter(&$content) {
   // Add a class to top-level items for styling.
-  foreach (element_children($content['menu']) as $link) {
-    $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
+  if (isset($content['menu'])) {
+    foreach (element_children($content['menu']) as $link) {
+      $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
+    }
   }
 
   // Alter icon.
-  unset($content['icon']['icon']['#theme']);
-  $content['icon']['icon']['#title'] = '<span>' . t('Home') . '</span>';
-  $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-category';
+  if (isset($content['icon'])) {
+    unset($content['icon']['icon']['#theme']);
+    $content['icon']['icon']['#title'] = '<span>' . t('Home') . '</span>';
+    $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-category';
+  }
 
   // Alter user account link.
-  $content['user']['account']['#title'] = t('Hello <strong>@username</strong>', array('@username' => $content['user']['account']['#title']));
-  $content['user']['account']['#options']['html'] = TRUE;
+  if (isset($content['account'])) {
+    $content['account']['account']['#title'] = t('Hello <strong>@username</strong>', array('@username' => $content['account']['account']['#title']));
+    $content['account']['account']['#options']['html'] = TRUE;
+  }
 }
 
-- 
1.7.11.msysgit.1

