Jump to content

Hook into Page::loaded in admin only


psy
 Share

Recommended Posts

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:

  1. ONLY applicable in admin
  2. ONLY applicable if in page edit on a page that will have front end output (not a custom admin page)
  3. 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...

$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

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

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