Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Events - pass in old data & and is_dirty boolean #7

Closed
edalzell opened this issue May 24, 2019 · 12 comments · Fixed by statamic/cms#5502
Closed

FR: Events - pass in old data & and is_dirty boolean #7

edalzell opened this issue May 24, 2019 · 12 comments · Fixed by statamic/cms#5502
Labels
feature request New feature or request

Comments

@edalzell
Copy link

edalzell commented May 24, 2019

There have been several times where it's very useful for a listener to check the old version to see if anything has changed (dirty).

Reason for this is that sometimes you have to update the data and therefore call $data->save(), which fires the events again. This creates an infinite loop.

I currently solve this by copying the core "save" code into my listener so the events aren't fired again.

Option would be able to call save with a "don't fire events" param? Or expose a deeper save method that is only in charge of saving and not firing events?

Any chance we could get that for v3?

@jackmcdade jackmcdade transferred this issue from statamic/cms Sep 19, 2019
@jackmcdade jackmcdade added the feature request New feature or request label Sep 19, 2019
@benfurfie
Copy link

+1 for this.

@edalzell
Copy link
Author

edalzell commented Jul 1, 2020

As per discussion w/ Jesse in Discord, it would be even better if the event was only fired when there's changed/new data.

@edalzell
Copy link
Author

edalzell commented Jul 1, 2020

Perhaps related: statamic/v2-hub#2520

@jesseleite
Copy link
Member

It might be better, just a thought.

Anyway, would like to see more practical examples of where you might actually use old data and/or dirty booleans in our data events? Providing old data to all of the events across our system could get messy.

@edalzell
Copy link
Author

edalzell commented Jul 1, 2020

Like I said above Jesse, it's to prevent a loop.

I'll try to find some code that I've used in the past.

@jesseleite
Copy link
Member

Theoretically, you'd be able to prevent loops by hooking into EntrySaving and cancelling a save. These aren't all implemented yet, but the plan is that every data type will fire these events...

  • EntrySaving (pre-save, with the ability to cancel save)
  • EntrySaved (post-save)
  • EntryDeleting (pre-delete, with the ability to cancel delete)
  • EntryDeleted (post-delete)

(The Saved and Deleted events are coming soon with the new git integration feature. The Saving and Deleting events aren't in there yet, but coming at some point.)

@edalzell
Copy link
Author

edalzell commented Jul 1, 2020

How would I know to cancel the save if I don't know if anything has changed? So either we have to know if something has changed, or the event can't fire when nothing has changed.

@jesseleite
Copy link
Member

Not saying that is the solution, just adding context to the discussion around events.

@jasonvarga
Copy link
Member

It's a lot simpler for Eloquent Models to check if they're dirty since they only have attributes and original arrays to worry about.

Perhaps the "saving" events are a better place for when you want to modify a thing. Then you don't have to re-save them.

@edalzell
Copy link
Author

edalzell commented Jul 3, 2020

I only want to things if the content has been saved though...validation passed, all Statamic stuff done etc.

@aerni
Copy link

aerni commented Feb 13, 2021

This would be a very much appreciated addition! I've come across this a lot where I needed to check if a value has been changed or not. And only perform some logic when it did change. Right now I'm working around this with either some boolean values in the entry data or with some cache. But both feel very dirty and unreliable.

@FrittenKeeZ
Copy link

A similar exists boolean prop to determine whether an entity was deleted would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants