Skip to content
Grav 2.0 is officially stable. Read the announcement →
Plugins

Page collection is empty when in admin panel

admin

Solved by pamtbaau View solution

Started by Martin Bekec 6 years ago · 10 replies · 1234 views
6 years ago

Hello,

I am developing a plugin where I need to access a page using its route.
Here is the code I use: $this->grav['pages']->find($this->viewRoute, true)

However when I try to do this in Admin Panel, it returns null.
After a bit of debugging I found out that $this->grav['pages'] does not contain any pages when in Admin Panel.

Is this supposed to happen? If so, how can I access pages when in Admin Panel? If not, what could be causing it?

Thanks in advance.

6 years ago

@MartinBekec, In which event are you checking the existence of the page?

When I hook up to event onPagesInitialized and check for $event['pages'] I get to see all available pages.

6 years ago

I originally did it with $this->grav['pages'] in onPageInitialized. Now I tried onPagesIntialized and $event['pages'].

Specifically, this is the code I tried:

PHP
public function onPagesInitialized(Event $event) {
    dump(array_keys($event['pages']->all()->toExtendedArray()));
}

When ran in Admin Panel it says: []
Outside Admin Panel it correctly lists all pages.

last edited 07/05/20 by Martin Bekec
6 years ago

@MartinBekec

See result of my code:

PHP
public function onPagesInitialized($event) {
   $page = $event['pages']->find('/typography', true);
   // $page contains a Page object of page '/typography'
   $paths = array_keys($event['pages']->all()->toExtendedArray());
   // $paths contains array
   // 0: "/home"
   // 1: "/typography"
}

6 years ago

Have you tried it in the Admin Panel?

I've tried your exact code (with dump to display it) and nothing changed.

This is how I register the event, in case that matters:

JS
public static function getSubscribedEvents() {
    return [
        'onPluginsInitialized' => [
            ['autoload', 100000], // TODO: Remove when plugin requires Grav >=1.7
            ['onPluginsInitialized', 0]
        ],
        'onPagesInitialized' => ['onPagesInitialized', 0]
    ];
}

PS: I also tried creating a new plugin, but that didn't work either.

6 years ago

@MartinBekec, Yes, I did...

Although it makes no difference for the results, I declare the events via:

PHP
public function onPluginsInitialized()
{
    // Don't proceed if we are in the admin plugin
    if ($this->isAdmin()) {
        $this->enable([
            // Put your main events here
            'onPagesInitialized' => ['onPagesInitialized', 0],
        ]);
        return;
    }

    // Enable the main events we are interested in
    $this->enable([
        // Put your main events here
    ]);
}

By declaring the events in onPluginsInitialized() you can make sure the plugin only runs when called by Admin, or by regular request.

It also made no difference whether the method is called using /admin, or /admin/pages, or /admin/pages/typography, or /admin/config/site, ...

6 years ago

@pamtbaau
I also tried putting the event in $this->enable, but as you mentioned, it doesn't make any difference.

Yes, normally I would disable it in the Admin Panel, the problem is however, that I need the plugin to intercept a post request from the Admin Panel (that works without any issue).

For me everything under /admin simply displays []
Everything not under /admin works.

Also, what version of Grav are you using? I am using 1.7 and am wondering if that could be the issue.

6 years ago

@MartinBekec, Interesting... When using Grav v.1.7 only null or [] values returned...

Maybe, opening an issue on Github might be an idea?

6 years ago Solution

@MartinBekec, Seems we have overlooked the migration docs 1.6 -> 1.7 which has a section on breaking changes for Admin plugins:

BC BREAK Admin will not initialize frontend pages anymore, this has been done to greatly speed up Admin plugin.

Please call $grav['admin']->enablePages() or {% do admin.enablePages() %} if you need to access frontend pages. This call can be safely made multiple times.

The following works in Grav 1.7.

PHP
public function onPagesInitialized($event) {
    $this->grav['admin']->enablePages();
    $page = $event['pages']->find('/typography', true);   // Correct Page object
}
👍 1
last edited 07/07/20 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 88 1 week ago
Plugins · by Xavier, 4 weeks ago
2 86 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1211 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 78 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 105 2 months ago