The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

Easy Site Administration for Both Site Builders banner

Coming up with a way to easily navigate and administer a Drupal site has always been a bit of a struggle. Usually, you’ve pleased the site builders and confused the normal site admins. Or pleased the site admins, but then caused the site builders to pull out their hair in frustration. We like to use admin_menu for site builders, and to use navbar as a nice and easy site admin toolbar. Admin_menu is great to give site builders quick access to almost every admin page there is. Navbar keeps things nice and simple – trying to avoid site admins getting overwhelmed with menu options, and potentially getting lost on their site. Each one works great for its target audience, but those target audiences aren’t the same, and most times, you’ll have both types of users actively using the site.

Here at Cheeky Monkey, we’ve found a way to keep both sides happy.

Our solution has been to combine the navbar module and the admin_menu module to play nice with each other. Normally, if you enabled both navbar and admin_menu, you’ll get them stepping on each other’s toes, and covering each other up, making neither one very usable. What we ended up doing was using admin_menu access priority. So if you have access to admin_menu, you only get the admin_menu. If you just have access to the navbar, you only get the navbar.

You will need to create a custom Drupal module, or add the code to a pre-existing custom module to have all of the magic happen. Below are the functions we use to get the two modules to play nice with each other. As you will see, we make use of a few commonly used hooks, and a few lesser known ones as well to ensure when we want to use admin_menu, there are no lingering references to navbar adding some markup or causing javascript errors. The following example code assumes you’ve created a module called system_settings.

/**
 * Implements hook_page_alter().
 */
function system_settings_page_alter(&$vars) {
  // Allow for both navbar and admin_menu to be enabled.
  // Full admins get admin_menu, others get navbar.
  if (isset($vars['page_top']['navbar'])) {
	// If the user can access the navbar, hide if they can see the admin menu.
	if (user_access('access navbar')) {
      $vars['page_top']['navbar']['#access'] = !user_access('access administration menu');
	}
  }
}
 
/**
 * Implements hook_js_alter().
 */
function system_settings_js_alter(&$js) {
  $javascript = &drupal_static('drupal_add_js', array());
  // Drop a navbar added function if user isn't using navbar.
  if (user_access('access administration menu')) {
	if (!empty($javascript['settings']['data'])) {
  	foreach ($javascript['settings']['data'] as $key => $setting) {
    	if (isset($setting['tableHeaderOffset']) && $setting['tableHeaderOffset'] == 'Drupal.navbar.height') {
          unset($javascript['settings']['data'][$key]);
    	}
  	}
	}
  }
}
 
/**
 * Implements hook_module_implements_alter().
 */
function system_settings_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'js_alter') {
	// Move our implementation to the end of the list, so it fires last.
    $group = $implementations['system_settings'];
    unset($implementations['system_settings']);
    $implementations['system_settings'] = $group;
  }
}

 

Adding this in has helped keep both our Monkeys and our clients quite happy, to have easy-to-use and easy-to-navigate sites when it comes to administration. And if you have a client site admin toolbar module you like to use, maybe this will give you some insight into how you might get it to play nice with admin_menu.

Need help with a big Drupal project? Our devs are available to help other agencies with everything from migration to maintenance. Call us!