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?
Although it makes no difference for the results, I declare the events via:
PHP
publicfunctiononPluginsInitialized(){// Don't proceed if we are in the admin pluginif($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, ...
@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.
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.