Jump to content

[Solved] Lock page with hook after/before Pages::saved?


rash
 Share

Recommended Posts

Hi all,

I’m trying to lock a page before or after save if a field has one of two specific values. Sounds easy, but keeps me busy since hours. My hook:

$wire->addHookAfter("Pages::saved", function($event) { 
	$page = $event->arguments(0);
	if ($page->template == "invoice") {
		$page->of(FALSE);
		// lock page if inv_status = paid (1034) or cancelled (1038)
		if ($page->inv_status == 1034 || $page->inv_status == 1038) {
			$page->setAndSave("status", "locked");
			// result: page gets locked, but I receive 'Service Unavailable'
			// I also tried this with the same result
			// $page->addStatus("locked");
			// $page->save();
		}
	}
});

The page gets locked but I receive this notorious 'Service Unavailable' message that starts to bore me a bit. Tried several other variants with identical results, the same goes for firing the hook before instead of after. Maybe it’s illogical to lock a page and save this status change immediately after, but when I don’t save the change, of course nothing at all happens.

Link to comment
Share on other sites

You have an endless loop happening - you're hooking Pages::saved, and then saving the page inside the hook, which calls Pages::saved, which saves the page, etc.

So you need some extra logic to exclude the page you are saving in the hook. In this case you can identify the page you want to exclude by checking if it is locked.

if ($page->template == "invoice" && !$page->isLocked()) {
    //...

Alternatively you can hook Pages::saveReady, then you don't need to worry about endless loops because you are hooking just before the page is about to be saved and therefore you don't need to save within the hook itself.

$wire->addHookAfter("Pages::saveReady", function($event) {
	$page = $event->arguments(0);
	if ($page->template == "invoice") {
		// lock page if inv_status = paid (1034) or cancelled (1038)
		if ($page->inv_status == 1034 || $page->inv_status == 1038) {
			$page->addStatus("locked");
		}
	}
});

 

  • Like 6
Link to comment
Share on other sites

Thanks a lot for you help, @Robin S – at least your first proposal ist working, while the alternative with Pages::saveReady doesn’t. Actually the loop problem is quite obvious, so shame on me for not detecting it myself, my brain sometimes seems to be somehow blocked. Anyway, just to consolidate the new enlightening: when I do $page->setAndSave("field", $value) before Pages::saved everything is fine. That’s because I’m saving field values whereas locking a page saves no field value but the page itself and therefore causes the loop. Is that correct?

Nevertheless would it be interesting to understand why your alternative isn’t working.

Link to comment
Share on other sites

12 hours ago, rash said:

Anyway, just to consolidate the new enlightening: when I do $page->setAndSave("field", $value) before Pages::saved everything is fine. That’s because I’m saving field values whereas locking a page saves no field value but the page itself and therefore causes the loop. Is that correct?

Yes, it's because if you specify a field then behind the scenes setAndSave() calls $pages->saveField(), and that doesn't trigger a Pages::saved hook. Whereas saving a property of the page that isn't a field (e.g. its name, status, etc) does require the saving of the page itself and therefore triggers a Pages::saved hook. But rather than having to remember these kinds of details I think in most cases you are better off using hooks to methods with "ready" in their name, e.g. Pages::saveReady, Pages::saveFieldReady and then you don't need to perform that same save action inside your hook.

12 hours ago, rash said:

Nevertheless would it be interesting to understand why your alternative isn’t working.

It should be possible to set "locked" status in a Pages::saveReady hook - it's working for me here. 

  • Like 1
Link to comment
Share on other sites

On 5/11/2021 at 12:23 AM, Robin S said:

It should be possible to set "locked" status in a Pages::saveReady hook - it's working for me here. 

Yes, it does for me here too. Started to work immediately after I eliminated a typing error in my template name. (In fact I didn’t assume one second you would post non-working code, but was pretty sure the problem is located in front of my display.) So another big thanks, that was very instructive.

  • Like 1
Link to comment
Share on other sites

  • rash changed the title to [Solved] Lock page with hook after/before Pages::saved?

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...