This thread started in the discord dev channel with @OleVik.
It seems similar to this thread:
I want to loop through pages in a CLI command. How should I access my pages?
This doesn;t work:
$grav = Grav::instance();
$pages = $grav["pages"];
if ($pages === null) {
$this->output->writeln("No pages.");
return;
}
It doesn’t return null, but I can’t use $page = $pages->find("/", true);
. It returns null
The $pages=
doen’t return null, but the ->find()
will return null
OleVik:
A normal
foreach
on$pages
?
For a concrete example, applicable to CLI: Plugin Recipes | Grav Documentation
@ OleVik Thanks for the suggestion. I tried foreach
(as per your suggestion) again, without result. This is the slimmed down code:
protected function serve()
{
$grav = Grav::instance();
$pages = $grav["pages"];
if ($pages === null) {
$this->output->writeln("No pages.");
return;
}
// dump( $pages );
dump ( 'Start foreach' );
foreach ($pages as $page) {
$this->output->writeln( $page->route() );
}
dump ( 'End foreach' );
}
The only thing you get to see is the output of the dump()
.
Once I get this working, I actually (also) need to find a parent page (e.g. ‘/blog’) first, and look through its children or descendants. The main loop nor the find are working atm.
@ OleVik A variant (from the example in the link you provided) also does not make a difference:
protected function serve()
{
$page = Grav::instance()['page'];
$pages = $page->evaluate(['@page.self' => '/']);
if ($pages === null) {
$this->output->writeln("No pages.");
return;
}
dump ( 'Start foreach' );
foreach ($pages as $page) {
$this->output->writeln( $page->rawRoute() );
}
dump ( 'End foreach' );
}
The $pages array appears to be empty in both cases?
OleVik:
Could you post the full PHP-file somewhere, or add it using the + button next to the textarea? I can’t immediately tell why.
@ OleVik I found a related forum post that suggests using $grav->process()
after instance()
, but that throws an error about a missing array.