Vayu Robins Posted July 14, 2015 Share Posted July 14, 2015 Hi. I am hooking into the saveReady hook in order to update data on other pages, when a page is published. In other words, when a transaction is published, the user is updated. Certain fields are reset on Publish for some reason. It's only on publish, so if I insert data into input fields again and press save, then it works. So i have added this bit of code to the admin.php file in my template. $page->addHook( 'Pages::saveReady', null, 'credit_published' ); function credit_published( $event ) { $page = $event->arguments[0]; if( $page->template == "credit-log" && $page->id && !$page->isTrash() && $page->isChanged( 'status' ) && !$page->is( Page::statusUnpublished ) && $page->credit_action->id === 3 ) { // Add credit to user. $partner = $page->partner; $partner->of( false ); $partner->credit = intval( $partner->credit + $page->credit ); $partner->save(); } } Link to comment Share on other sites More sharing options...
Soma Posted July 14, 2015 Share Posted July 14, 2015 You don't need to save when hooking saveReady as PW does it already right after hook. Link to comment Share on other sites More sharing options...
Vayu Robins Posted July 14, 2015 Author Share Posted July 14, 2015 Thanks Soma, but I need to update another page (a partner page) when a transaction page is published. I use $partner->save() to do this. If uncomment this, then the problem disappears. What could be wrong with the code? Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 14, 2015 Share Posted July 14, 2015 How about using the Pages::published hook? You don't need to save when hooking saveReady as PW does it already right after hook. He's saving "another" page, so nope, it's needed there. Link to comment Share on other sites More sharing options...
Vayu Robins Posted July 14, 2015 Author Share Posted July 14, 2015 In the input field called "Partner" I get the user. I then save the user into the variable $partner. I then get the amount stored in the input field called "Kredit". I add the credit to the users credit. Link to comment Share on other sites More sharing options...
Soma Posted July 14, 2015 Share Posted July 14, 2015 A save will uncache all pages and you loose data.. so maybe not use save ready but after save. Link to comment Share on other sites More sharing options...
Vayu Robins Posted July 14, 2015 Author Share Posted July 14, 2015 Soma, is that the $page->addHookAfter( 'Pages::saved', null, 'credit_published' ); you refer to? LostKobraKai, is that the $page->addHookAfter( 'Page::published', null, 'credit_published' ); you refer to? I will try. Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 14, 2015 Share Posted July 14, 2015 Soma, is that the $page->addHookAfter( 'Pages::saved', null, 'credit_published' ); you refer to? LostKobraKai, is that the $page->addHookAfter( 'Page::published', null, 'credit_published' ); you refer to? I will try. It's Pages::published. I've corrected it already in my previous post, but it seems you didn't notice. Link to comment Share on other sites More sharing options...
Vayu Robins Posted July 15, 2015 Author Share Posted July 15, 2015 Thank you very much. So far so good... I can't check for status change the same way, but I think maybe I don't need that here. Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 15, 2015 Share Posted July 15, 2015 Nope, the hook is explicitly only called if a page is getting published. Link to comment Share on other sites More sharing options...
Vayu Robins Posted July 15, 2015 Author Share Posted July 15, 2015 That should perfect! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now