We've all used the admin_menu module for quite some time in Drupal, yet personally there are several things that made me jump ship from Admin Menu as soon as the first alpha release of Admin was available from Development Seed. I find admin_menu clunky, providing too many options, and always popping down menus every time I hover past the menu in the top of the browser window.
The only drawback I ever had when using the Admin module was the ability to quickly flush caches from the interface, which was one of my favorite tools in admin_menu. My solution was to take some of the great code from admin_menu, and port it to a more usable system for my own purposes. I had a release that was working with the first release of Admin that created menu items, and placed them with the admin group of items, and had to use the menu translate hook in order to append destinations to the menu items. It was a bit clunky, but worked for me on several sites, including this one.
However, with the release of the Admin 2 module, and the Rubik administration theme, I needed to redo the module so that it would operate as a custom block in the admin toolbar interface much in the way that the Devel module does in the demonstration.
In creating this module, I'm not clear yet wether it should be something that once perfected is wrapped into the Admin 2 module, or something that should be released in the Drupal contrib repository, so for now I'm hosing the project on Github just so I have more than a working copy floating around in a couple of my projects.
/**
* Implementation of hook_menu().
*/
function admin_clearcache_menu() {
$items['admin/flush-cache/%'] = array(
'title' => t('Clear Cache'),
'page callback' => 'admin_clearcache_flush_cache',
'access arguments' => array('administer site configuration'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Flush all caches or a specific one.
*
* @param $name
* (optional) Name of cache to flush.
*/
function admin_clearcache_flush_cache($name = NULL) {
switch ($name) {
case 'menu':
if(module_exists('views')) {
views_invalidate_cache();
}
menu_rebuild();
drupal_set_message('Menu Cache Cleared');
break;
case 'cache':
// Don't clear cache_form - in-progress form submissions may break.
// Ordered so clearing the page cache will always be the last action.
$core = array('cache', 'cache_block', 'cache_filter', 'cache_page');
$cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
foreach ($cache_tables as $table) {
cache_clear_all('*', $table, TRUE);
}
drupal_set_message('Cache Tables Cleared');
break;
case 'requisites':
// Change query-strings on css/js files to enforce reload for all users.
_drupal_flush_css_js();
drupal_clear_css_cache();
drupal_clear_js_cache();
drupal_set_message('CSS/JS Cache Cleared');
break;
case 'theme':
module_invoke('system', 'theme_data');
drupal_rebuild_theme_registry();
drupal_set_message('Theme Registry Rebuilt');
break;
case 'views':
views_flush_caches();
drupal_set_message('Views Cache Cleared');
break;
default:
// Flush all caches; no need to re-implement this.
module_load_include('inc', 'system', 'system.admin');
$form = $form_state = array();
system_clear_cache_submit($form, $form_state);
break;
}
drupal_goto();
}
function admin_clearcache_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks['clearcache'] = array(
'info' => t('Admin Tools'),
'weight' => 0,
'status' => 1,
'admin' => TRUE,
);
return $blocks;
}
else if ($op == 'configure' && $delta == 'clearcache') {
return $form;
}
else if ($op == 'save' && $delta == 'clearcache') {
}
else if ($op == 'view') {
switch($delta) {
case 'clearcache':
$block = array('subject' => t('Admin Tools'),
'content' => admin_clearcache_admin_content());
break;
}
return $block;
}
}
/**
* Function to build out content for the block that will be used in the administrative toolbar area
*/
function admin_clearcache_admin_content () {
$destination = drupal_get_destination();
$links = array(
'all' => array(
'title' => 'Clear All Caches',
'href' => 'admin/flush-cache/all',
'query' => $destination,
),
'views' => array(
'title' => 'Views Cache',
'href' => 'admin/flush-cache/views',
'query' => $destination,
),
'menu' => array(
'title' => 'Menu Cache',
'href' => 'admin/flush-cache/menu',
'query' => $destination,
),
'theme' => array(
'title' => 'Theme Registry',
'href' => 'admin/flush-cache/theme',
'query' => $destination,
),
'requisites' => array(
'title' => 'CSS/JS Cache',
'href' => 'admin/flush-cache/requisites',
'query' => $destination,
),
'tables' => array(
'title' => 'Cache Tables',
'href' => 'admin/flush-cache/cache',
'query' => $destination,
),
'cron' => array(
'title' => 'Run Cron',
'href' => 'admin/reports/status/run-cron',
'query' => $destination,
),
'update' => array(
'title' => 'Run Updates',
'href' => 'update.php',
),
);
$content = theme('links', $links, array('class' => 'menu'));
return $content;
}
?>
Above is the code I'm currently using. There is no doubt there are probably cleaner ways to implement the block & menu, but for now it works for me, and makes me happy to have back my favorite tools without enabling the admin_menu module. The admin_clearcache_flush_cache() function is a modified version of the function used in admin_menu to provide the cache clearing abilities.
One clear thing that can change is using the multiple menu items. It was needed in the prior version, yet this time it could be done using a single entry with a wildcard rather than a single entry for each item. That will come in a later update to clean it up a bit.
If anyone is interested in furthering this project, and touching up the code, feel free to work off of the public github repo, and open issues there.



Nice Addition
I think most people would want this feature and it should be bundled with Admin 2.0.
Thanks for taking the time to do this and providing a post about it.
Thanks!
I did just update this post to be a bit cleaner on the code, and remove the multiple menu items, and just setup the single menu callback using a wildcard.
This change was also commited to github as well.
The thought of this getting possibly wrapped into something else is what has kept me from commiting it to Drupal.org, as I feel it doesn't deserve it's own module really, and isn't something I really want to spend a TON of time on at any point. It was more of a stop gap solution for me to dump admin_menu 100% and get my little tools menu back!
Thanks for the kind words!
Jake Strawn
Drupal Rockstar
admin 2
Thanks for introducing me to admin 2. I haven't tried it, but have seen some screencasts. Wow! Very nice.
I've installed your module
I've installed your module (with Admin 2 and Rubik/Tao). But your module does not display its block on the Admin toolbar.
Did I miss something? :-)
I too loves Admin 2 and miss the clear cache links that Admin Menu provides. So I welcome your module. You should post it as a plug-in on the Admin project page as a feature request.
Check the settings...
I'd check to see if the block is enabled on the admin page for the admin module: admin/settings/admin.
It should have a checkbox beside it if it is going to show up in the toolbar. If that IS checked, and it's still not showing, try flushing caches the normal way, or mabye visiting admin/build/menu to ensure the menu cache is rebuilt. If that's not it, I'm not sure off the top of my head. I may have to investigateon a clean setup.
Jake Strawn
Drupal Rockstar
The Admin Settings
Indeed, this is the step I missed:
admin/settings/admin -> Enable blocks -> Admin Tools.
This is awesome.
Thanks.
Awesome!!
Glad that worked! I was thinking that it would automatically select anything that had the admin -> TRUE flag in the block settings, but I guess not.
I may want to set up an install for this that will auto flag that option in the settings to avoid further confusion, or at the very least, put a drupal_set_message in there for anyone that can manage the toolbar, and alert them where/how to turn it on so it's a bit more clear right out of the gate.
Jake Strawn
Drupal Rockstar
Project on Drupal org
This is a nice addition to Admin 2.
Will you put this project on Drupal.org? You would need to brand it as an Admin 2 "plug-in", though.
I'd love to see this on Drupal.org to track updates easily and post issues. :-)
I agree
I agree this should probably go to Drupal.org. It may eventually be pulled if it can get wrapped into the main Admin project, but I'll work on getting it updated in the next couple of days.
I want to rename the .module file, etc to something other than admin_clearcache since it can/does/will go a tiny bit beyond just flushing caches.
I'll post an update here, and probably a new post when I've got that updated, and launched on D.O
Jake Strawn
Drupal Rockstar
Post new comment