Hooks: page.update

I’d like to flush the cache of a page in a plugin as soon as it got updated. So I’m digging into hooks. Looking at https://getkirby.com/docs/reference/plugins/extensions/hooks#available-events there are quite a few, but it’s not very clear to me in which context they are called – especially the page.update hooks.

While there is page.update there are also page.changeNum, page.changeSlug, page.changeStatus, page.changeTemplate and page.changeTitle that also update the page. And if I consider that files are also related to the page, file changes actually update the page context as well.

Is page.update actually a general hook for all updates or is it considered to be a “page.changeContent” hook only (which does not exist looking at the names)?

The page update hook is only called when the page content is updated. Each action triggers a different hook, as you can see here in the PageActions commit() method:

 protected function commit(string $action, array $arguments, Closure $callback)
    {
        $old = $this->hardcopy();

        $this->rules()->$action(...$arguments);
        $this->kirby()->trigger('page.' . $action . ':before', ...$arguments);
        $result = $callback(...$arguments);
        $this->kirby()->trigger('page.' . $action . ':after', $result, $old);
        $this->kirby()->cache('pages')->flush();
        return $result;
    }

Where $action is can be changeStatus, update, changeSlug etc. That is only for pages. The same happens for site, file` and user hooks.

As far as I know, there are currently no wildcards for hooks like they existed in Kirby 2, but I think there is already an issue for that.

The advantage of these fine-tuned hooks is that you can determine to the point on what you want to react on.

Here’s the link to the issue in the ideas repo:

Thanks, @texnixe! You are of great help – as usual.
I didn’t know that wildcard hooks existed in earlier version. They sound helpful.

My guess is this is due to historical reasons but having a hook named page.update that is actually “only” firing on page.changeContent is counterintuitive and should be renamed (which is certainly too late for version 3 due to semantic versioning but it’s semantically wrong and misleading in my eyes).

At least it is in line with the page->update() method that does nothing but updating the page content. Naming methods is always difficult… I understand that it might be misleading for people who haven’t worked with these methods right from the beginning and are just used to it.

You are right about the alignment with the method name, that’s of course important as well.

As I said above, I don’t expect a name change happening soon although it would be logical to me. When trying to understand the given logic, one starts to notice that must be steeped in tradition (“historisch gewachsen”). Maybe adding a note to the docs why this is so (backwards compatibility) and how the page.update differs from it’s name would be helpful for new Kirby users starting with version 3.

1 Like