In a grav theme plugin, I want to provide a method to fill a select input field in a page blueprint with some other page's routes:
video_route:
type: select
data-options@: '\Grav\Theme\MyTheme::videoRoutes'
Those pages must be descendants of another specific page, called 'videos', here. My theme plugin method looks like this:
public static function videoRoutes() : array
{
$pages = new Pages(Grav::instance());
$pages->init();
$collection = $pages->getCollection(['items' => ['@page.descendants' => '/videos']]);
[...]
}
Inside the Grav\Common\Page\Pages::getCollection() method, the 'videos' object is found and instanciated, and the format of the params array seems also correct. But the resulting collection is empty. What is wrong with my approach?
Try the following:
public static function videoRoutes()
{
$grav = Grav::instance();
$grav['admin']->enablePages();
$pages = $grav['pages'];
$collection = $pages->getCollection(['items' => ['@page.descendants' => '/videos']]);
[...]
}
Admin->enablePages()
calls Pages->enablePages()
. According the docs:
public enablePages() : void
Method used in admin to later load frontend pages.
Please be advised that if you use above code inside a theme installed from Grav's theme repo, you will lose all changes on next update of the theme. You should in that case create an inherited theme, or create a plugin.