1. Home
  2. Blog
  3. Contact
Blogitems
31Mar

Pimp my Backend: Extending the "clear cache" menu

Posted on March 31st 2010 at 09:00 inside Extension DevelopmentExtensionsRealURL: Clear cache with 11 comments

I will use an example extension that I have created prior to writing this article. It's allows you to clear RealURL's cache quickly, through the "Clear cache" menu. Which might be useful for testing or reconfiguring.

Why useful?

Besides RealURL, there might be other things you would like to be able to clear through the "Clear cache" menu. Like cached elements that are specific to your extension. Of course we should avoid something that should be manually cleared, however could be great for testing, configuring and dummy websites.

So what do we need to do?

To accomplish this nifty little trick, we need to start using some hooks! Well, actually we need just 1 hook :-) I'm using the following one:

The hook I utilized 

  1. $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions']
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions']

It's called from "typo3/classes/class.clearcachemenu.php", somewhere around line 93 (source version 4.3.2). I will utilize this hook in my own extension by adding a class reference to my ext_localconf.php.

The class reference 

  1. $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'][] = 'EXT:realurl_clearcache/class.tx_realurlclearcache_cachemenu.php:&tx_realurlclearcache_cachemenu';
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'][] = 'EXT:realurl_clearcache/class.tx_realurlclearcache_cachemenu.php:&tx_realurlclearcache_cachemenu';

Of course, in order to make this work I created the file "class.tx_realurlclearcache_cachemenu.php" containing the class "tx_realurlclearcache_cachemenu.php" and the hook function "manipulateCacheActions". The code (included at the bottom of this article) is very small and shouldn't be to hard to understand. Basically we configure our new button, with a title, icon and a target. The interesting part is the target. Because at some point, we should actually execute some code.

Displaying the icon 

  1. $s_title = $GLOBALS['LANG']->sL('LLL:EXT:realurl_clearcache/locallang.xml:rm.clearCacheMenu_realUrlClearCache', true);
  2. $s_imagePath = $GLOBALS['TYPO3_LOADED_EXT']['realurl_clearcache']['siteRelPath'];
  3. if(strpos($s_imagePath,'typo3conf') !== false) $s_imagePath = '../'.$s_imagePath;
  4. $a_cacheActions[] = array(
  5.     'id'    => 'realurl_cache',
  6.     'title' => $s_title,
  7.     'href' => 'ajax.php?ajaxID=tx_realurlclearcache::clear',
  8.     'icon'  => '<img src="'.$s_imagePath.'be_icon.gif" title="'.$s_title.'" alt="'.$s_title.'" />',
  9. );
$s_title = $GLOBALS['LANG']->sL('LLL:EXT:realurl_clearcache/locallang.xml:rm.clearCacheMenu_realUrlClearCache', true);
$s_imagePath = $GLOBALS['TYPO3_LOADED_EXT']['realurl_clearcache']['siteRelPath'];
if(strpos($s_imagePath,'typo3conf') !== false) $s_imagePath = '../'.$s_imagePath;
$a_cacheActions[] = array(
    'id'    => 'realurl_cache',
    'title' => $s_title,
    'href' => 'ajax.php?ajaxID=tx_realurlclearcache::clear',
    'icon'  => '<img src="'.$s_imagePath.'be_icon.gif" title="'.$s_title.'" alt="'.$s_title.'" />',
);

AJAX!

The reason you never see any reloads in your back-end after clearing caches is because it's done through the help of Ajax. We will of course also use this method. I took a quick peek at the clear RTE cache option that has been add since 4.3. In the code above you saw that we refered to the following:

The AJAX reference 

  1. 'ajax.php?ajaxID=tx_realurlclearcache::clear'
'ajax.php?ajaxID=tx_realurlclearcache::clear'

If your a little more familiar with static methods in PHP classes, you will see that "tx_realurlclearcache::clear" is actually a function call (or in this case a reference). Now this doesn't of course just work from itself, we will need to do a little more magic voodoo ;-)

First of all, we need to make a new class/file with a function that will handle our cache clearing. I created "class.tx_realurlclearcache.php" with the class "tx_realurlclearcache" and function "clear". What we need to do next is register the function, let the Ajax handler know what we want to do when "tx_realurlclearcache::clear" is called.

This is done in the same was as adding configuring hooks in your extensions. We simply set a class reference for the Ajax handler in our extensions' ext_localconf.php.

Adding the AJAX reference 

  1. $TYPO3_CONF_VARS['BE']['AJAX']['tx_realurlclearcache::clear'] = 'EXT:realurl_clearcache/class.tx_realurlclearcache.php:tx_realurlclearcache->clear';
$TYPO3_CONF_VARS['BE']['AJAX']['tx_realurlclearcache::clear'] = 'EXT:realurl_clearcache/class.tx_realurlclearcache.php:tx_realurlclearcache->clear';

The Result

By default it's only enabled for admins, but with the following User TSconfig value you are able to add it for editors as well.

Enabling the clear cache option for editors 

  1. options.clearCache.realurl = 1
options.clearCache.realurl = 1

Conclusion

Adding new cache clearing options is a piece of cake! This proves once again that it's not always so hard to configure your back-end to your own needs. Something similar can be made in probably less than 30 minutes (if you are slow ;-)), if you are interested you can use my extension as an example. It's included at the bottom of this article (which should be underneath this text ;-)). Later on I will also attempt to upload it to the repository.

Update!

Due to quite some requests I have decided to finalize this extension and publish it to the TER. The most recent stable version is available at the following location.

http://typo3.org/extensions/repository/view/realurl_clearcache/current/

Bugs and feature requests may be posted on the TYPO3 Forge, there this project is available under the following location:

http://forge.typo3.org/projects/show/extension-realurl_clearcache

Tags

typo3 extension development clear cache extend realurl

Comments (11)

Gravatar: Benjamin SerfhosBenjamin Serfhos2010-03-31 09:44

Nice functionality!

Can you show a screenshot of what this should look like? I am curious, is this also for the editor as backend user?

Keep up the interesting tuts!

Gravatar: Sebastiaan de JongeSebastiaan de Jonge2010-03-31 09:57

You're absolutely right Ben, my bad :-)



Updated the article, see the "Result" part. By default only enabled for admins, but through User TSconfig configurable to work for editors as well.

Gravatar: Remco RaaijmakersRemco Raaijmakers2010-03-31 10:46

Nice work.

Easy and quick way to clear the cache.

Gravatar: Nitin SewtahalsingNitin Sewtahalsing2010-03-31 12:20

Great article!



Are you going to publish this to the TER?

Gravatar: AlexandreAlexandre2010-03-31 16:18

Do you know that your RSS feed has stopped to update since january 12th ? I'd like to read you via my feed reader !

Gravatar: Sebastiaan de JongeSebastiaan de Jonge2010-03-31 19:50

@Nitin: Yes I am planning to do so :-) Will write a tiny little manual and do some finetuning



@Chris, @Remco: Thanks! :-)



@Alexandre: No I wasn't aware of that, the file is supposed to be cached but I guess for some reason the rewriting stopped. It's fixed now, thanks for the notice :-)

Gravatar: chris Zepernickchris Zepernick2010-06-14 09:06

nice work man!

Gravatar: ChristianChristian2014-05-15 11:30

TYPO3 v6.2 Compatibility

Hi Sebastiaan,
thank you for this nice usefull extension. Are there any plans in updating it so it will work in TYPO3 v6.2 LTS?
Christian

Gravatar: SebastiaanSebastiaan2014-05-15 12:54

RE: TYPO3 v6.2 Compatibility

Yes, I actually am. Planning on doing so in the coming week. :-)

Gravatar: GünterGünter2014-07-15 16:37

Icon for editor at TYPO3 4.5 missing

Hi, does anyone have a hint how to enable the delete icon for an editor at TYPO3 4.5
Unfortunately the configuration as described in the docs doesn't work.

Your comment