Jump to content

'noHooks' option in $page->save()


MarkE
 Share

Recommended Posts

I had been struggling to implement the 'noHooks' option in $page->save(). 

$page->save(['noHooks => true']) does not seem to work, nor does $pages->save($p, ['noHooks => true'])

Then I found the following documentation in wire/core/Pages.php:

	 * - `noHooks` (boolean): Prevent before/after save hooks (default=false), please also use $pages->___save() for call.

So $pages->___save($p, ['noHooks => true']) does work.

Just thought I'd mention it as it is a bit buried (and a bit unusual). Also, I'm not sure if it is possible to use the noHooks option with $page->save().

  • Like 2
Link to comment
Share on other sites

10 hours ago, MarkE said:

$page->save(['noHooks => true']) does not seem to work, nor does $pages->save($p, ['noHooks => true'])

Not sure if it's just a forum typo but you should only have quotes around the key - it should be:

$page->save(['noHooks' => true]);

It seems to be working here for me.

 

10 hours ago, MarkE said:

Then I found the following documentation in wire/core/Pages.php

The PhpDoc comments form the basis of the online documentation: https://processwire.com/api/ref/pages/save/
So you're right that it wouldn't be hard to miss but it's not buried in the sense that you'd only find it by examining the core files.

 

10 hours ago, MarkE said:
please also use $pages->___save() for call

As a general thing, if you call any hookable method and include the underscores at the start of the method name then any hooks to that method won't be triggered. So I think the idea here is that the noHooks option takes care of making sure that several potential downstream hooks wont fire (e.g. Pages::saveReady, Pages::savePageOrFieldReady, Pages::saved, Pages::savedPageOrField, Pages::added) but in case Pages::save was itself hooked you'd need to call it with the underscores to prevent that hook from firing.

But what's interesting is that the core itself doesn't call Pages::___save (with the underscores) when you do call Page::save() with the noHooks option true, and likewise with Pages::___saveField. Maybe the documentation for the noHooks option should be expanded to explain exactly which hooks are avoided, or the core should call Pages::___save and Pages::___saveField with underscores when noHooks is true. @ryan

  • Like 1
Link to comment
Share on other sites

12 minutes ago, Robin S said:

Not sure if it's just a forum typo

It was. The code was correct.

13 minutes ago, Robin S said:

It seems to be working here for me

That's funny - I'll investigate further and post any info. I did a debug_backtrace in Tracy which clearly showed the hook being called despite 'noHooks'  - maybe there was something else interfering.

Link to comment
Share on other sites

Thanks for your help @Robin S

I sort-of get it: 

$page->save(['noHooks' => true]);

just prevents hooks running on Page::save - it does not prevent downstream hooks on Pages::save etc. My hook was on before Pages::save as advised elsewhere on this forum. See image below (ready() is needed to load the hook, debug_backtrace is running in the beforeSaveThis hook on Pages::save).

844674700_noHooksissue.thumb.jpg.38341f8e0f20e5932515433bdb673a2a.jpg

However, if I try

$pages->save($page, ['noHooks' => true]);

then the hook on Pages::save still runs! EDIT: Ironically, this still only prevents hooks running on Page::save

76391694_noHooksissue2.thumb.jpg.3b420de3430c18e0d39efa10c08112b7.jpg

but 

$pages->___save($page, ['noHooks' => true]);

works, as does plain

$pages->___save($page);

This all seems pretty confusing and inconsistent though (at least to my little brain). Perhaps @ryanor someone with greater knowledge than me could shed some more light on this (and clarify the documentation?).

  • Like 1
Link to comment
Share on other sites

15 hours ago, MarkE said:

I sort-of get it: 

$page->save(['noHooks' => true]);

just prevents hooks running on Page::save - it does not prevent downstream hooks on Pages::save etc.

No, it does prevent a bunch of downstream hooks from firing, but specifically it doesn't include Pages::save and Pages::saveField (and I think it should include those methods). And to avoid confusion it would be good if the documentation explained exactly which hookable methods are affected by the noHooks option when it used in any of Page:save(), Pages::save() and Pages::saveField().

I've opened a GitHub issue so that Ryan is more likely to notice the problem and our discussion of it: https://github.com/processwire/processwire-issues/issues/1405

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...