Grav: Can't get all pages from plugin's CLI

Created on 30 Jan 2018  路  1Comment  路  Source: getgrav/grav

I have a simple CLI script for my Grav plugin like this

<?php
namespace Grav\Plugin\Console;

use Grav\Console\ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;

class ImportCommand extends ConsoleCommand {
  protected function configure() {
    $this->setName('import');
  }

  protected function serve() {
    $pages = Grav::instance()['pages']->all();
  }
}

However when I execute bin/plugin import import from terminal, I get this error:

$ bin/plugin import import
PHP Fatal error:  Uncaught Error: Call to a member function root() on null in /var/www/html/my-site/system/src/Grav/Common/Page/Pages.php:570
Stack trace:
#0 /var/www/html/my-site/user/plugins/place-import/cli/ImportCommand.php(17): Grav\Common\Page\Pages->all()
#1 /var/www/html/my-site/system/src/Grav/Console/ConsoleCommand.php(29): Grav\Plugin\Console\ImportCommand->serve()
#2 /var/www/html/my-site/vendor/symfony/console/Command/Command.php(266): Grav\Console\ConsoleCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /var/www/html/my-site/vendor/symfony/console/Application.php(875): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /var/www/html/my-site/vendor/symfony/console/Application.php(204): Symfony\Component\Console\Application->doRunCommand(Object(Grav\Plugin\Console\ImportCommand), Object(Symfony\Component\Console in /var/www/html/my-site/system/src/Grav/Common/Page/Pages.php on line 570

With the above code I can get the pages from the plugin's main file but if put that code in CLI script, it doesn't work. I have Grav 1.3.10.

I have also tried to create a new Collection but I get a similar error, root page can't be found.

If I want to get all the pages from CLI script, what is the best way to do? Thanks in advance!

question

Most helpful comment

I believe the problem is that you are trying to access Pages before they are initialized. In the CLI you have to do this manually. See this plugin for an example: https://github.com/trilbymedia/grav-plugin-tntsearch/blob/develop/cli/IndexerCommand.php

>All comments

I believe the problem is that you are trying to access Pages before they are initialized. In the CLI you have to do this manually. See this plugin for an example: https://github.com/trilbymedia/grav-plugin-tntsearch/blob/develop/cli/IndexerCommand.php

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RyanMPierson picture RyanMPierson  路  3Comments

Gong-Bao-Chicken picture Gong-Bao-Chicken  路  5Comments

adrianschembri picture adrianschembri  路  3Comments

josefstr picture josefstr  路  4Comments

matroidde picture matroidde  路  3Comments