How to trigger file->update() in two plugins that use the same hook?

Hi,

I’m using the ColorExtractor plugin by @sylvainjule ,

which uses a file.create.after hook to update the color field of a file.

Now, in my own plugin I also need to update a field of a file with a file.create.after hook,
but this breaks the update in the ColorExtractor plugin. The color field stays empty.

How can I use both plugins?

Hm, the hooks are both triggered, but it seems the last one wins with regard to updating the file meta data. Not sure if this is a bug or a feature.

1 Like

Hm, ok.
For now, I’ve edited the core of the ColorExtractor plugin and return the $hex value instead of calling $image->update(), and then calling the extractColor method in my own plugin.
Not an ideal solution, but it works for now.
Anyway, need to test this behaviour with some other hooks…
Thanks Sonja for having a look into it!

I wonder if it worked in an older Kirby 3 version… will check if I find the time. Maybe it’s worth creating an issue because these things are hard to debug if you install multiple plugins that use the same hook to do the same thing.

1 Like

I tested with some older versions now but with the same result. While both hooks are definitely called (i.e. if you use them to create a log file), updating from both ignores the first hook.

What are you trying to do in your plugin hook? Maybe there is another way to achieve what you want.

Kirby’s objects are immutable. That means, when you modify an object like $page , $file etc. using a method like update() , changeTitle() and so on, a new object is returned. Therefore, you have to store the returned object in a new variable to be able to further work with it.

Looks like hooks do not pass the new object to the next hook. Therefore both work with the initial $file object. Every $file->update() call overwrites previous changes with the intial content and the current updates.

2 Likes

I encounter the same situation, also using the colorExtractor plugin together with my own plugin that reads file meta data and should store it to fields. But is overwritten.

To me it sounds more like bug than feature, because the concept @moeli described would mean, that it is conceptually unsafe or impossible to run multiple plugins at once. Should someone create an issue?

1 Like
1 Like

Thanks @texnixe for trying to find another solution for this problem and sorry for not getting back to You earlier (had to place this issue on hold since I was working on a project with a tight deadline…).
Honestly I already can’t remember exactly what I was trying to do.
I think it had to sth. to do with this issue, trying to update a field value after uploading a file.

Thanks @moritzebeling for creating the issue on Github!