Jump to content

How to execute some logic when a page is saved IF some field values have changed


TomPich
 Share

Recommended Posts

I recently had the following need :
For pages of a given template (named
page-post-operation), I had to check if a certain field value had changed (a checkbox field named postReadyForPublish) and execute some logic if that was the case (in my case, send an e-mail to an admin user with proper rights to publish it).

I thought, at the beginning, that it would be a little bit tricky as I needed to compare the value of this field for the previous version and the new version of the page.
And it actually was not tricky. Kind of... ?

So this is the hook I put at the beginning of the /site/templates/admin.php file (as it’s only useful in the admin part).

$wire->addHookBefore("Pages::saveReady(template=page-post-operation)", function($event) {
	$page = $event->arguments(0);
	$oldPage = $this->pages->getFresh($page->id); // this is the tricky part
	if( $page->postReadyForPublish && !$oldPage->postReadyForPublish ){
		// logic goes here
	}
});

When you retrieve the previous version of the page in the DB, make sure you use the getFresh method. Otherwise, you will get the date in the cached version of the page, which are immediately saved, meaning that $oldPage and $page would have the same field values.

Hope this may be useful to somebody else.

  • Like 4
Link to comment
Share on other sites

Hey @TomPich I think what you did can also be done like this: https://processwire.com/blog/posts/new-ajax-driven-inputs-conditional-hooks-template-family-settings-and-more/#new-conditional-hooks

$wire->addHookAfter('Page(template=order)::changed(order_status)', function($event) {
    // execute some code when "order_status" changes on "order" pages.
});

 

  • Like 2
Link to comment
Share on other sites

Posted (edited)

Woaw... Even simplier ! I didn’t know about the Page::changed hook. ?

Thanks Bernhard.

EDIT: I just realized that, in my use case, it wouldn’t be enough. I don’t want to execute the logic if the field has changed, but [IF the field has changes AND the new field value is true]. But still, this hook is great and may cover 90% of use cases.

Edited by TomPich
See EDIT part.
  • Like 1
Link to comment
Share on other sites

@netcarver yep, that's also implemented: https://github.com/baumrock/RockMigrations/blob/be4138ed26d8aa0116ebdd21a79148d21574d546/MagicPages.module.php#L191-L202

<?php
// in site/classes/OrderPage.php
public function onChanged($field, $old, $new) {
  if($field == 'order_status') {
    if($new == 'foo') $this->sendFooMail();
    elseif($new == 'bar') $this->sendBarMail();
  }
}

 

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