Page sort hook triggered on every update

I’m working on a Git plugin alternative and need to listen to every page and file hook in order use the correct commit message. The issue is that every time an update is made, the panel.page.sort hook is triggered before panel.page.update and is the one that get it’s message used.

From my research, this is caused because the updateNum() method (which then calls the sort() method) is called before the update hook inside the update() method at panel/app/src/panel/models/page.php.

I understand it’s important to call the updateNum() method there just in case for when the date is being used to order pages. I have quick fix in mind that would not trigger the sort hook on any update, is a pull request welcome?

Another thing that would greatly benefit plugin developer would be to always pass the old and new page object to hooks. When constructing the Page class Kirby could store the old page before updates are applied. Would that be feasible?

1 Like

Yes, very welcome. Please make sure however that the sort hook is triggered if the sorting number actually changes. And if it doesn’t, it shouldn’t be triggered of course. :slight_smile:

The panel.page.move hook already has this feature. It is implemented by cloning the object before modifying it. I agree that the same could be done for the other hooks as well (except for create and delete hooks).

1 Like

I suggested this not only thinking about my use case, it would allow developer to do any kind of check they need before doing something in the hook. It won’t break existing hooks because the $old object would be the last argument (ignored if not used). Please, let’s do this!

1 Like

Yeah, I agree, it would be very helpful indeed for other use cases as well. E.g. Listen to certain page changes in hooks

2 Likes

I think I’dreaming, but no. This is Kirby’s world, full of awesome surprises!

Well, @bastianallgeier already took care of this in v2.3.0. Now the sort hook is only triggered when the order actually changes!

2 Likes

As I said, I fully agree. I just wanted to point out how the move hook currently does this so that we can do it in a similar way for the other hooks. A PR would be great here!

1 Like

Here you go! Hope to see it added in v2.3.0.

Please let me know if you spot any issue.

While we are at it, I found an issue with the upload hook and submitted a pull request:

1 Like