Skip to content

Adding check for hooks that should run - #352

Closed
dougblackjr wants to merge 2 commits into
ExpressionEngine:stabilityfrom
dougblackjr:bug/248-hook-firing-twice
Closed

dougblackjr wants to merge 2 commits into
ExpressionEngine:stabilityfrom
dougblackjr:bug/248-hook-firing-twice

Conversation

@dougblackjr

Copy link
Copy Markdown
Contributor

Overview

This addresses #248, where the after_channel_entry_save extension hook would run twice when saving a new channel entry.

Resolves #248 .

Nature of This Change

  • 🐛 Fixes a bug
  • 🚀 Implements a new feature
  • 🛁 Refactors existing code
  • 💅 Fixes coding style
  • ✅ Adds tests
  • 👽 Adds new dependency
  • 🔥 Removes unused files / code
  • 🔒 Improves security

Is this backwards compatible?

  • Yes
  • No

Documentation

From what I see, this hook was firing twice due to the save function in EllisLab\ExpressionEngine\Service\Model\Model.php. The save function fires the hook immediately upon invoking this function. When creating a new channel entry, the insert method is called on the facade. It appears that, when this happens, it runs through updateEntryStats in ChannelEntry.php, which then reruns the save function above, but this time updates the entry. The save hooks are being called twice.

The check I added checks against the specific object that is created to see if it is processed; if the save hook has already run, it won't run it again.

I'm not entirely sure this is the most elegant solution, so I gladly welcome any feedback.

@litzinger

Copy link
Copy Markdown
Contributor

I feel like adding a global change like this could have adverse side effects. If the issue is that updateEntryStats is calling save() a second time, perhaps adding a parameter to the save function to tell it not to forward the hooks, e.g. save(false), and then just don't call $this->forwardEventToHooks(); or, add a property to the model, e.g. $forwardToEventHooks, and then create a setter and chain the methods in the updateEntryStats() method, e.g. $stats->setForwardToEventHooks(false)->save();. A change like that would ensure nothing is accidentally broken and makes the action explicit.

@dougblackjr

Copy link
Copy Markdown
Contributor Author

@litzinger That was my concern as well. My only concern with adding it to the save function is that change would be even more global, and have a lot more possible impact. Save exists on the Model class, which is involved just about everywhere. I chose this route because it seemed to be the least impactful, and specific only to this extension hook call. What do you think?

@litzinger

Copy link
Copy Markdown
Contributor

Well, regardless of where it is added, I feel like setting the extra property on the class is safer, b/c it would default to false and require a very explicit call to prevent forwarding to the hooks.

@dougblackjr

Copy link
Copy Markdown
Contributor Author

Closing in favor of #386

@dougblackjr dougblackjr closed this Apr 1, 2020
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.

The after_channel_entry_save event hook is called twice for every add-on using it

2 participants