Iterate pages with CLI command

You need to wait until Pages can be made available, then initialize them. Eg.:

/* Initialize Grav and Pages */
$grav = Grav::instance();
$grav->fireEvent('onPagesInitialized');
$grav['pages']->init();
$pages = $grav['pages']->all();
// Report how many Pages there are
dump(count($pages));

/* If we only want a subset of pages, let's evaluate a route */
$page = Grav::instance()['page'];
$pages = $page->evaluate([
    '@page.children' => $this->options['route']
]);
$pages = $pages->published()->order('date', 'desc');
$paths = array();
// Iterate through Pages, store some properties
foreach ($pages as $page) {
    $route = $page->rawRoute();
    $path = $page->path();
    $title = $page->title();
    $paths[$route] = $title;
}
// Report those properties
dump($paths);
exit();
return;

With some sample Pages this yields:

C:\Caddy\Test
λ php bin/plugin cli-test scan /book
9
array:6 [
  "/book/postscript" => "Postscript"
  "/book/the-queens-crocquet-ground" => "The Queen’s Croquet-Ground"
  "/book/a-mad-tea-party" => "A Mad Tea-Party"
  "/book/were-all-mad-here" => "We're all mad here"
  "/book/advice-from-a-caterpillar" => "Advice from a Caterpillar"
  "/book/down-the-rabbit-hole" => "Down the Rabbit-Hole"
]
2 Likes