Skip to content

Comments

Call model methods using event handler with default priority#150

Merged
LukeTowers merged 13 commits intodevelopfrom
call-model-methods-from-event-handlers
Aug 13, 2023
Merged

Call model methods using event handler with default priority#150
LukeTowers merged 13 commits intodevelopfrom
call-model-methods-from-event-handlers

Conversation

@mjauvin
Copy link
Member

@mjauvin mjauvin commented Aug 7, 2023

Replaces #145

This preserves current behavior: an event listener defined on the model with default priority (0) will get called first, then the model method will get called (through an event listener with default priority).

If an event listener uses a LOWER priority (e.g. -1), the model method will get called first (because its priority is 0 by default)

Note: there is still one thing I'd like to get clarified: an event listener defined in a plugin's boot method with default priority(0) will get called BEFORE the listener that calls the model method... so that means the Plugin's event listeners get registered FIRST, before the event listeners defined in the Model's bootNicerEvents() method... anyone care to explain why that is ?

My guess is that Plugin's boot() method gets called before any model are instanciated, which makes sense, right?

@mjauvin mjauvin added this to the v1.2.4 milestone Aug 7, 2023
@mjauvin mjauvin self-assigned this Aug 7, 2023
@mjauvin mjauvin changed the title Call model method using event handler with default priority Call model methods using event handler with default priority Aug 7, 2023
@LukeTowers
Copy link
Member

@bennothommo @jaxwilko any final thoughts on this?

@mjauvin
Copy link
Member Author

mjauvin commented Aug 9, 2023

I still need to apply this to other places in the code base, but didn't want to invest the time until this is accepted as a solution.

@mjauvin
Copy link
Member Author

mjauvin commented Aug 9, 2023

I added halt:true to the fireEvent() call, but this might break BC if some existing listener returns a value since that will bypass the model method (which wasn't the case before).

So I think we shouldn't be using a halting event. But if we don't, then we will break things anyway because it wont't be possible anymore to cancel an action even with the model method.

Thoughts ?

@mjauvin
Copy link
Member Author

mjauvin commented Aug 9, 2023

Just to be clear: currently, you cannot prevent the action through an event hanlder. For example, if you return false from the beforeSave() method on a model, the save action will get cancelled... but if you return false from a listener on the model.beforeSave event, the action will still go through.

Now, it WOULD be nice if both methods would behave the same, but this will definitely break things in the most unexpected ways...

I checked all winter plugins and core, there are no cases where an event handler actually returns a value on the before{Create,Update,Save,Delete} events, and definitely nothing returns false.

@bennothommo
Copy link
Member

I think there's always been an implicit knowledge that events, in general, will cancel any further functionality when false is returned, so the likelihood that people have accidentally returned a false when they shouldn't have is low.

The event docs for models explicitly state that you should return false in callback methods to cancel the action, and there's nothing else that I've seen that contradicts that, so I would say that both the callbacks and the events should work the same.

@mjauvin
Copy link
Member Author

mjauvin commented Aug 10, 2023

I think there's always been an implicit knowledge that events, in general, will cancel any further functionality when false is returned, so the likelihood that people have accidentally returned a false when they shouldn't have is low.

The event docs for models explicitly state that you should return false in callback methods to cancel the action, and there's nothing else that I've seen that contradicts that, so I would say that both the callbacks and the events should work the same.

I agree, this is the saner way to go and will set things straight.

mjauvin and others added 3 commits August 10, 2023 09:08
Co-authored-by: Luke Towers <github@luketowers.ca>
Co-authored-by: Luke Towers <github@luketowers.ca>
@mjauvin
Copy link
Member Author

mjauvin commented Aug 10, 2023

@LukeTowers @bennothommo should I now go ahead and apply the above for the Validation & SoftDelete Traits and the Halcyon model ?

@mjauvin
Copy link
Member Author

mjauvin commented Aug 10, 2023

@bennothommo I don't understand the latest Code Analysis errors.

@mjauvin mjauvin requested a review from LukeTowers August 10, 2023 23:10
@LukeTowers
Copy link
Member

@bennothommo could you take a look?

@bennothommo
Copy link
Member

@mjauvin @LukeTowers for future reference, if you get an error like that from PHPStan ("Ignored error pattern..."), it means that an error we put in the baseline to be ignored originally is no longer occurring, so we just need to remove the error pattern that follows from phpstan-baseline.neon as I have. This is a good thing - the less we have being ignored in the baseline, the better.

@mjauvin
Copy link
Member Author

mjauvin commented Aug 13, 2023

Ok I'm going to add unit tests for this, any suggestion/preference ?

@LukeTowers LukeTowers merged commit 4f683cc into develop Aug 13, 2023
@LukeTowers LukeTowers deleted the call-model-methods-from-event-handlers branch August 13, 2023 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants