Jump to content

Clearing Cache on Module Save


Pete
 Share

Recommended Posts

I'm working on a module where I use MarkupCache within the module.

Normally you would use MarkupCache in a template, but I fancied putting it in the module as it needs to use some 3rd party classes and it's just a lot of code to dump into a template.

What the module does is my own version of pulling and caching X tweets from Twitter. The problem I had was clearing the cache when I'd changed the number of tweets to store in the module's config and saved the config.

With help from netcarver it became apparent that you can hook into saveModuleConfigData, but it then took me a while to work out how to hook it.

The answer was in InputFieldPageName.module on lines 134-7:

static public function getModuleConfigInputfields(array $data) {

$modules = wire('modules');
$modules->addHookBefore('saveModuleConfigData', null, 'InputfieldPageName_saveModuleConfigData');

So not in the init() where I'd been trying to put it, but in the getModuleConfigInputfields function instead.

Because it's here though, you have to put the function that's called outsite of the class' scope, but that's not an issue really. In that same module you can scroll to the bottom and after the last closing curly-brace is this:

function InputfieldPageName_saveModuleConfigData(HookEvent $event) {
$arguments = $event->arguments;
if($arguments[0] != 'InputfieldPageName') return;
$data = $arguments[1];
$name = 'replacements';
if(!is_array($data[$name])) $data[$name] = InputfieldPageName::replacementStringToArray($data[$name]);
$arguments[1] = $data;
$event->arguments = $arguments;
}

You can still use the wire class from in there as well, so you can still pretty much do what you like. In my case it was this simple two-liner (setting the cache to 0 seconds effectively wipes it):

 $cache = wire('modules')->get("MarkupCache");
$cache->get("latest_tweets", 0);

Just thought that might be useful to someone else in future :)

So thanks again to netcarver, and also to ryan for having had the need to do this before and making saveModuleConfigData hookable!

  • Like 4
Link to comment
Share on other sites

  • 1 year later...

With help from netcarver it became apparent that you can hook into saveModuleConfigData, but it then took me a while to work out how to hook it.

The answer was in InputFieldPageName.module on lines 134-7:

static public function getModuleConfigInputfields(array $data) {

$modules = wire('modules');
$modules->addHookBefore('saveModuleConfigData', null, 'InputfieldPageName_saveModuleConfigData');

So not in the init() where I'd been trying to put it, but in the getModuleConfigInputfields function instead.

Because it's here though, you have to put the function that's called outsite of the class' scope, but that's not an issue really. In that same module you can scroll to the bottom and after the last closing curly-brace is this:

function InputfieldPageName_saveModuleConfigData(HookEvent $event) {
$arguments = $event->arguments;
if($arguments[0] != 'InputfieldPageName') return;
$data = $arguments[1];
$name = 'replacements';
if(!is_array($data[$name])) $data[$name] = InputfieldPageName::replacementStringToArray($data[$name]);
$arguments[1] = $data;
$event->arguments = $arguments;
}

Thanks, just what i was looking for. Needed to check if a path on module config save, that can be set on the module configuration, exists, has certain subfolders and files and is readable.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...