Monday, December 10, 2012

Clear cache URL or menu link

Create a new page, with the input filter set to a filter that can run PHP. Then use the following snippet, which was shamelessly yanked from devel.module:
<?php/*
* 2008 Jun 26
*
* The code submitted by dharmanerd in his comment probably
* works better than the original I had, so I've modified this
* to match his code.
*
* The original code for this was shamelessly yanked from
* `devel.module`; it was the function `devel_cache_clear()`.
*
*/

// only allow site administrators to visit this page:
if (!user_access('administer site configuration')) {
 
drupal_not_found();
}
else {
 
drupal_clear_css_cache();
 
$tables = array(
   
'cache',
   
'cache_content',
   
'cache_filter',
   
'cache_menu',
   
'cache_page',
   
'cache_views',
  );
  foreach (
$tables as $table) {
   
cache_clear_all('*', $table, TRUE);
  }
 
drupal_set_message('Cache cleared.');
 
drupal_goto();
}
?>
Set a URL path for this page (perhaps 'cache_clear') so that you can clear the cache easily. Clear the cache by visiting the URL or add the path to a menu link(watch your permissions).
Code Notes:
  • The drupal_goto at the end of the page will redirect you to either the destination or to the main page of your site; you can change this if you wish.
  • If this code is in a block that was visible on all pages (i.e. including the home page) then the home page would endlessly redirect to itself, breaking your site. The answer is to remove the drupal_goto() from the code, then all caches would be cleared on every page view.
  • Because of the drupal_goto, you can never actually visit this page; you can edit it through the admin/content/node page.
  • You may have to remove the path parameter to the function call drupal_goto, because it gets added to the homepage URL.
  • If you missed some part of the code, which makes the site inaccessible you can remove the code through phpmyadmin, by browsing the block table and editing the appropriate block you created.

1 comment:

only show translated menu items into current language (Drupal 8)

function MY_THEME_preprocess_menu(&$variables) {   if ($variables['menu_name'] == 'brancott-header-menu') {    $langu...