Skip to content

Simplify post hook example, and make example code more efficient, too.

Rich requested to merge artfulrobot/dev:artfulrobot-post-hook into master

The example code - which I know has been copied into several extensions! - was OK but was a bit cumbersome.

It went along the lines of:

  • implement a generic hook function
  • test for a current transaction or not, and then pass on to a second generic hook function
  • implement the second generic hook function and then test whether we need to do anything or not
  • then do the do

My replacement code is much more succinct and puts the "do we need to do this" test first, so we don't have to bother with other function calls if this hook is not for us.

I see quite a lot of hooks code (daresay I'm guilty too) whereby the author forgets that this hook is very generic, and thinks only about the particular goal they're working on. As a result, quite a bit of inefficiency is added, like db lookups on custom fields etc. that could just be short-circuited early. So it seemed important to weed this out of the docs.

Merge request reports