Mackski Posted December 5, 2013 Share Posted December 5, 2013 This might seem a trivial question to you long time PW users, I'm still getting my head around things. I need to add a hook, after values have been populated to do some calcuations from selected fields. I'm struggling to work out exactly where to add my hook. Is there a detailed breakdown of the order of method execution in PW, in relation to loading a page?My code is as follows: $this->addHook('ProcessPageEdit::buildForm', $this, 'totalBooking'); // calculate and show the total booking amount in a given field public function totalBooking(HookEvent $event) { $total = 0; $page = $event->arguments('page'); if($page->template != "booking") return; $total = $page->booking_delivery_cost; if(count($page->booking_items) > 1 ) { foreach($page->booking_items as $item) { $total += ($item->booking_item_cost * $item->booking_qty); } } if($page->booking_discount > 0) { $total -= $page->booking_discount; } $page->booking_total_amount = $total; return; } Link to comment Share on other sites More sharing options...
Soma Posted December 5, 2013 Share Posted December 5, 2013 I think looks good except it should be addHookAfter. You add a new method with addHook. Link to comment Share on other sites More sharing options...
Mackski Posted December 5, 2013 Author Share Posted December 5, 2013 It doesn't seem to work, here are the hook methods I've tried: $this->addHookBefore('ProcessPageEdit::buildForm', $this, 'totalBooking'); $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'totalBooking');$this->addHook('ProcessPageEdit::buildForm', $this, 'totalBooking');I can save the field like this: $this->addHookBefore('Pages::saveReady', $this, 'totalBooking');But I don't want to re-save a few hundred pages, just have it calculate dyanmically at page load. Link to comment Share on other sites More sharing options...
Soma Posted December 5, 2013 Share Posted December 5, 2013 And further ___buildForm recieves and returns a $form (InputfieldForm) object and not a page. So you have to do like: $form = $event->return; to get the form To get the page ProcessPageEdit is currently editing, you can use the getPage() method that you find in ProcessPageEdit #1128 $page = $event->object->getPage(); $event->object in the hook represents the module you hook into, that would be the ProcessPageEdit.module. Hope that makes sense and helps. And not it's not that trivial even for long time users. 2 Link to comment Share on other sites More sharing options...
Mackski Posted December 5, 2013 Author Share Posted December 5, 2013 Thanks Soma,That's fixed it. Could there be a better hook point, ie: Page::loaded ? for these type of operations? Link to comment Share on other sites More sharing options...
Soma Posted December 6, 2013 Share Posted December 6, 2013 I'm not sure I fully understand what you expect or what this calculation is meant for on every load when editing a page, because when you change a value and save hte page, your calculated value is maybe still a wrong one. ProcessPageEdit is a perfect place to alter fields in context of a edit page in admin. But yes there's a Page::loaded. Edit: sorry for the edit, there's so many hooks and cases for what you can use them and where it makes sense or not. Often there's a lot of different ways to achieve the same, but not all maybe fit the case. Link to comment Share on other sites More sharing options...
Mackski Posted December 6, 2013 Author Share Posted December 6, 2013 The page is a book form, it has a number of repeaters for products. I'm just totalling them, and applying some calculations, ie: delivery costs, discounts etc. and displaying the order total in a dummy field for user reference.As stated, I could use Page::saveReady, but it means I have to re-save all existing pages.This way, I can just calculate it on-the-fly, as it's something the user does not modify directly. Link to comment Share on other sites More sharing options...
Soma Posted December 6, 2013 Share Posted December 6, 2013 How is re-saving a problem? set_time_limit(0);// in a template $books = $pages->find("template=book"); foreach ($books as $book) { $book->of(false); $book->save(array("quiet" => true)); echo "<p>Saved : $book->title</p>" } echo "<p>Done!</p>"; 2 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