Jump to content
psy

Hook into Page::loaded in admin only

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

Share this post


Link to post
Share on other sites

On mobile...

$page->template == 'admin'
$page->process == 'ProcessPageEdit'

  • Like 1

Share this post


Link to post
Share on other sites

@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

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Maybe just add your hook in some hook of ProcessPageEdit, which is executed before your hook shall be triggered.

  • Like 1

Share this post


Link to post
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 😊

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...