Home > Development, English, php, Techie > Building a cache cleaner from devel for Drupal 5

Building a cache cleaner from devel for Drupal 5

In a particular context, it happened to me to have to reproduce a cache cleaner feature for Drupal 5 without installing the Devel module.

Although this article doesn’t create anything new, I thought it might be worth explaining in case this would help someone someday.

The idea is to re-use the code from the devel module and put it into another module for re-use without having the weight corresponding to a new module installation.

The code from the devel module (devel.module) looks like this:

/**
* Implementation of hook_menu().
*/
function devel_menu($may_cache) {
$items = array();

if ($may_cache) {
$items[] = array(‘path’ => ‘devel/cache/clear’,
‘title’ => t(‘Empty cache’),
‘callback’ => ‘devel_cache_clear’,
‘access’ => user_access(‘access devel information’),
‘type’ => MENU_CALLBACK,
);

// … the rest of this hook is of no interest to us

The only thing we would need to modify here is the permission, to avoid creating a new permission just for this. For example, we could use the permission “access administration menu” which comes from the admin_menu module (and hack it along to add the feature).

Then the corresponding function to clean the cache looks like this:

/**
* Menu callback; clears all caches, then redirects to the previous page.
*/
function devel_cache_clear() {
// clear preprocessor cache
drupal_clear_css_cache();

// clear core tables
$core = array(‘cache’, ‘cache_filter’, ‘cache_menu’, ‘cache_page’);
$alltables = array_merge($core, module_invoke_all(‘devel_caches’));
foreach ($alltables as $table) {
cache_clear_all(‘*’, $table, TRUE);
}
drupal_set_message(‘Cache cleared.’);
drupal_goto();
}

The good thing about this function is that all the functions used in there are Drupal Core functions, so practically, declaring this menu item in any other module will make the feature available and usable.

So, how do we do it?

First, get the devel_cache_clear() function into the admin_menu/admin_menu.module and rename it to admin_menu_cache_clear(). There might be another function called admin_menu_clear_cache() over there, but the name is subtly different and you will only use it temporarily, so don’t fear.

/**
* Menu callback; clears all caches, then redirects to the previous page.
*/
function admin_menu_cache_clear() {
// clear preprocessor cache
drupal_clear_css_cache();

// clear core tables
$core = array(‘cache’, ‘cache_filter’, ‘cache_menu’, ‘cache_page’);
$alltables = array_merge($core, array());
foreach ($alltables as $table) {
cache_clear_all(‘*’, $table, TRUE);
}
drupal_set_message(‘Cache cleared.’);
drupal_goto();
}

Note that I have removed a module_invoke_all(‘devel_cache’) here (because devel does not exist).

Next, update the menu element to add a link to this function:

admin_menu_add_item($admin_menu, $mid_admin, array(
‘title’ =>    ‘Clear cache’,
‘path’ =>     ‘admin/user/clearcache’,
‘callback’ => ‘admin_menu_cache_clear’,
‘weight’ =>   -89,
‘type’ => MENU_CALLBACK,
));

The weight will allow the menu element to appear next to the leftmost element (very visible). Depending on the form of your menu, this element might rather look like this:

$items[] = array(
‘title’ =>    ‘Clear cache’,
‘path’ =>     ‘admin/user/clearcache’,
‘callback’ => ‘admin_menu_cache_clear’,
‘weight’ =>   -89,
‘type’ => MENU_CALLBACK,
);

Now make sure you don’t leave a syntax error around, save the module and reload your portal. You should now see the element appear in the menu, and clicking it should execute the cache clean command.

Clear cache option appears in admin-menu, without devel

Clear cache option appears in admin-menu, without devel module installed

You will also get onto an empty “users” page, but that’s not really of my concern for this quick hack. Ideally, you would have made a copy of the module before, and you would comment this new code once you don’t need it anymore. Finally, you would add an access control of the kind of “if(user_access(‘administer users’))” or whatever permission you feel OK with assigning this feature to.

Watch out when copy-pasting the code above, as the apostrophes seem to be transformed in non-apostrophes when pasting them, which produces syntax errors!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: