psy Posted February 14, 2019 Share Posted February 14, 2019 I'm well into developing a module that has both front and admin features so don't want to flag the module as 'admin only'. One hook has very specific requirements: ONLY applicable in admin ONLY applicable if in page edit on a page that will have front end output (not a custom admin page) ONLY applicable if the page has a particular Fieldtype which is a custom field type specific to this module suite I've got 2 & 3 down but am stuck trying to figure out 1 despite trying lots of things. When viewing the page on the front end, the hook activates and crashes the page. public function ready() { $this->addHookAfter('Page::loaded', $this, 'hookResultJson'); } public function hookResultJson (HookEvent $event) { $page = $event->object; // Only applicable in admin area in page edit mode // STUCK ON THIS ONE ????? // Confirm the page has a FieldtypeMyCustom field $fields = $this->wire('fields'); $paFields = $fields->find('type=FieldtypeMyCustom'); if (!$paFields->count) return; $widgetFld = ''; foreach ($paFields as $item) { if ($page->hasField($item->name)) { $widgetFld = $fields->get("name=".$item->name); break; } } // No field of FieldtypeMyCustom on the page/template if (empty($widgetFld)) { return; }; // We are now in admin edit of a page that has a field of type FieldtypeMyCustom // ... } Any suggestions for the 'admin only page edit' conditional statement? TIA psy Link to comment Share on other sites More sharing options...
matjazp Posted February 14, 2019 Share Posted February 14, 2019 On mobile... $page->template == 'admin' $page->process == 'ProcessPageEdit' 1 Link to comment Share on other sites More sharing options...
psy Posted February 14, 2019 Author Share Posted February 14, 2019 @matjazp Thanks, tried both and they didn't work. ? $page->template == 'admin' meant it affected the page list $page->process is 'Process Template' for both front and back end, including page list - maybe due to hook method Page::loaded Link to comment Share on other sites More sharing options...
psy Posted February 14, 2019 Author Share Posted February 14, 2019 More... $page->template == 'admin' Works BUT it interferes with the receipt of the WireHttp call to get the json result. It always comes back as null. Works fine when I remove this conditional from the hook, but then the front end falls over. I supposed I could put a conditional further down to see if the http result is valid but am concerned that will dramatically slow down front end page load. The test could only be done after the GET call and a bunch of database hokey pokey. Would be much more efficient to have it at the start of the hook - if it's the front end, return now; if it's admin, process the GET call etc Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 14, 2019 Share Posted February 14, 2019 Maybe just add your hook in some hook of ProcessPageEdit, which is executed before your hook shall be triggered. 1 Link to comment Share on other sites More sharing options...
psy Posted February 14, 2019 Author Share Posted February 14, 2019 Thanks for the suggestions. I ended up putting the conditional further down the food chain for now. It works and with page caching, shouldn't affect the front end page render speed. Maybe in a later release I'll fine tune it a bit more ? 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