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.
@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.
I originally did it with $this->grav['pages']
in onPageInitialized. Now I tried onPagesIntialized and $event['pages']
.
Specifically, this is the code I tried:
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.
@MartinBekec
See result of my code:
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"
}
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:
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.
@MartinBekec, Yes, I did…
Although it makes no difference for the results, I declare the events via:
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
, …
@anon76427325
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.
@MartinBekec, Interesting… When using Grav v.1.7 only null
or []
values returned…
Maybe, opening an issue on Github might be an idea?
@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.
public function onPagesInitialized($event) {
$this->grav['admin']->enablePages();
$page = $event['pages']->find('/typography', true); // Correct Page object
}
1 Like