jncs Posted July 24, 2019 Share Posted July 24, 2019 Hi everyone, i tried a little bit with hooks. I Update a Page in Pages::saveReady and Try to do Stuff with Hanna Code in Pages::saved I realized that. the $page Object is the Problem cause of: $page $page2 = wire('pages')->find($page->id)[0], Are in the Hook Method ( After Pages::saved) not the same. Why is this so? And how i can get arround this? (Version 3.0.98) Thanks a lot jncs Link to comment Share on other sites More sharing options...
Edison Posted July 24, 2019 Share Posted July 24, 2019 Hi @jncs, how do you retrieve $page from within Pages::saved ? Not sure this will help you. It is how to get $page in the hook and return it after it has been manipulated: $this->addHookBefore('Pages::saved', function(HookEvent $event) { $pages = $event->object; $page = $event->arguments(0); // add here your modifications to $page fields $event->arguments(0, $page); }); Link to comment Share on other sites More sharing options...
jncs Posted July 24, 2019 Author Share Posted July 24, 2019 (edited) Hi @Edison, thank you for your replay. Here my Code, (a little shortend). public function init() { $this->addHookAfter('Pages::saveReady', $this, 'doStuffBeforeOrderIsSaved'); $this->addHookAfter('Pages::saved', $this, 'doStuffAfterOrderIsSaved'); } function doStuffBeforeOrderIsSaved($event) { $page = $event->arguments('page'); if($page->template != "my_template") return; if($page->isUnpublished()) return; bd($page, "Datenübername"); if($this->session->get("email") != null) $this->addAdressAndName($page); } function addAdressAndName($page){ $page->of(false); $page->email = $this->session->get("email"); // etc.... } function doStuffAfterOrderIsSaved($event) { $page = $event->arguments('page'); $changes = $event->arguments('changes'); if($page->template != "my_template") return; if($page->isUnpublished()) return; if(array_key_exists('customer_status',$changes)) { $this->sendMail($this->message, $page); } } public function sendMail($message,$page) { bd($page,"object from event"); bd(wire('pages')->find($page->id)[0],"object from find"); $mail = wireMail(); $hanna = wire('modules')->get('TextformatterHannaCode'); $hanna->page = $page; $subject = $message->message_header; $bcc = $message->message_bcc; $from = $message->message_from; $text = $hanna->render($message->message_text,$page); $mail->to($page->email); $mail->bcc($bcc); $mail->from($from); $mail->subject($subject); $mail->bodyHTML($text); $mail->body(strip_tags($text)); $mail->send(); } In sendMail Hanna Code gets not the freshly Saved Object, but the old one, without the updatet Data. Also $page->email is not the updated one. But in the Output from tracy the first page object seems to be right. It Only apperes if the data is updatet via doStuffBeforeOrderIsSaved and then used in doStuffAfterOrderIsSaved. Is this an caching Problem? Can i get arround this by invalading the cache for this object, and how to do that? Thanks a lot. Edited July 24, 2019 by jncs typo Link to comment Share on other sites More sharing options...
Edison Posted July 26, 2019 Share Posted July 26, 2019 Hi @jncs, sorry for taking sometime to reply you, it was a busy week. I just replicated your code in my environment and tested it. I created your two hooks Pages::saveReady and Pages::saved in ready.php and works without any issues. I tested it supplying a constant value to $page->email (my field equivalent). I suspect the issue you are encountering may be somehow related to the modified value storage in the session ($this->session->get("email")) or, as you suggested, it may be affected by cache. If the issue depends from this session variable it should be interesting to trace its contents. I am sure you have good reasons to use session storage, but you may also consider an $email property to your class module (of course the approach is different, but may be useful to validate if the issue depends from session/cache). 1 Link to comment Share on other sites More sharing options...
jncs Posted July 27, 2019 Author Share Posted July 27, 2019 Hi @Edison, thank you for your efford, The Session is not manipulated at this point, it is saved from a form much earlier and not touched again. I think the Problem ist: $page wire('pages')->find($page->id)[0] Isn't the same object, not after "Page::saved" nor after "Page:save". Also wire('pages')->uncacheAll(); Has no effect. Have you an Idea where i can hook after a page is saved and it is realy saved? Link to comment Share on other sites More sharing options...
jncs Posted July 27, 2019 Author Share Posted July 27, 2019 Ok i don't now what is going wrong, but i seems to be some things i have installed, i need to test this longer, thanks a lot. 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