By Sebastiaan de Jonge, published on Wednesday, March 31, 2010 at 07:00

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:

$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.

$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.

$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:

'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.

$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.

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

Comments

Benjamin Serfhos
Benjamin Serfhos - Wednesday, March 31, 2010 at 07: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!

Sebastiaan de Jonge
Sebastiaan de Jonge - Wednesday, March 31, 2010 at 07: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.

Remco Raaijmakers
Remco Raaijmakers - Wednesday, March 31, 2010 at 08:46

Nice work.
Easy and quick way to clear the cache.

Nitin Sewtahalsing
Nitin Sewtahalsing - Wednesday, March 31, 2010 at 10:20

Great article!

Are you going to publish this to the TER?

chris Zepernick
chris Zepernick - Monday, June 14, 2010 at 07:06

nice work man!

Alexandre
Alexandre - Wednesday, March 31, 2010 at 14: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 !

Sebastiaan de Jonge
Sebastiaan de Jonge - Wednesday, March 31, 2010 at 17: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 🙂

Alex
Alex - Friday, February 3, 2012 at 20:54

Nearly two years passed since you wrote this and it still made my day!Thanks a lot!

Christian
Christian - Thursday, May 15, 2014 at 09:30

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

Sebastiaan
Sebastiaan - Thursday, May 15, 2014 at 10:54

Yes, I actually am. Planning on doing so in the coming week. 🙂

Günter
Günter - Tuesday, July 15, 2014 at 14:37

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

Nino
Nino - Friday, September 26, 2014 at 14:10

I found this problem in TYPO3 4.7.19 too so i made a little change inside /ext/realurl_clearcache/class.tx_realurlclearcache.php file. In isInsideDbList function I changed this line

if(substr($s_scriptName,$i_pathLength-11) == 'db_list.php') return true;

to

if(substr($s_scriptName,$i_pathLength-7) == 'mod.php') return true;

I'm not 100% sure this is the right way but it works for me.