Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

How get all pages in plugin for admin

plugin plugins

Solved by Daniel View solution

Started by Daniel 5 years ago · 5 replies · 923 views
5 years ago

Hello,
I'm creating my own plugin for working with data and it doesn't work for me to load an overview of all pages:

PHP
    public static function getSubscribedEvents() {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0],
        ];
    }

    public function onPluginsInitialized(): void
    {
        // Don't proceed if we are not in the admin plugin
        if (!$this->isAdmin()) {
            return;
        }

        $basename= $this->grav['uri']->basename();
        $uri = $this->grav['uri'];
        $config = $this->config();

        $route = $config['route'] ?? null;
        $route == $uri->path();
        $this->grav['debugger']->addMessage($route);

        if ($basename == 'contacts') {
            $this->enable([
                'onPageInitialized' => ['onPageInitialized', 0]
            ]);
        }
    }

    public function onPageInitialized($event): void
    {

        $this->grav['admin']->enablePages();   

        $grav = Grav::instance();
        $this->grav->fireEvent('onPagesInitialized',$event);
        $this->grav['admin']->grav['pages']->init();
        $page = $this->grav['admin']->grav['pages']; 

        $this->grav['debugger']->addMessage('-----------------------------------------');
        $this->grav['debugger']->addMessage($page);
        $this->grav['debugger']->addMessage('-----------------------------------------');

        //$paths = $event['pages']->find('/', true); - null
        //$paths = $event['pages']; - null
        //$page = $this->grav['admin']->grav['pages']->find('/'); - OK
    }

when i call grav['pages] so I'll get back:
image|690x24

but I only need a array of pages with a header and content, what am I doing wrong?

5 years ago

@danielkoptak, The code you are sharing contains quite a bit of debris and throws Exceptions. Please edit the post and provide the community with some clean relevant code, free from Exceptions and reproducing the issue you are experiencing.

Currently, your code does not reproduce the error you are showing, but instead is throwing an Exception on $this->grav['admin']->enablePages();.

Please help the community help you...

As a hint to a simple solution, please have a look at the Grav Lifecycle:

  1. Fire onPagesInitialized event with [pages]
PHP
public function onPagesInitialized($event): void
{
    /** @var Pages */
    $pages = $event['pages'];
}
5 years ago Solution

@pamtbaau:
$event['pages'];

Ok, my fault, for me works:

PHP
    public function onPagesInitialized($event): void
    {
        $this->grav['admin']->enablePages();   
        $pages = $event['pages']->all();
    }
5 years ago

@danielkoptak, When I use your code, the solution cannot work and will throw an Exception.

In your first post, you initialize the event callbacks when plugin is NOT being called by the Admin plugin. Therefor Admin is not added to the Grav container and $this->grav['admin'] will throw an Exception: Identifier "admin" is not defined.

Only when event onPagesInitialized is raised when running Admin, the code will succeed.

PHP
if ($this->isAdmin()) {
    $this->enable([
       // Put your main events here
       'onPagesInitialized' => ['onPagesInitialized', 0],
    ]);

    return;
}

If the code snippet you share does not represent the code you actually use, please correct it as asked before.

5 years ago

all my code looks like this:

PHP
<?php
namespace Grav\Plugin;

use Grav\Common\Plugin;
use Grav\Common\Page\Collection;
use Grav\Common\Uri;
use Grav\Common\Grav;

class dataJsonPlugin extends Plugin
{

    public static function getSubscribedEvents() {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0],
        ];
    }

    public function onPluginsInitialized(): void
    {
        // Don't proceed if we are not in the admin plugin
        if (!$this->isAdmin()) {
            return;
        }

        $basename= $this->grav['uri']->basename();

        if ($basename == 'contacts') {
            $this->enable([
                'onPagesInitialized' => ['onPagesInitialized', 0]
            ]);
        }
    }

    public function onPagesInitialized($event): void
    {
        $this->grav['admin']->enablePages();   

        $pages = $event['pages']->all();
        foreach ($pages as $page) {
            dump($page->header()->title);
        }

        exit();
    }

}

and everything works fine...
Grav v1.7.16
PHP v7.4.14
Admin v1.10.16
all cache disabled

5 years ago

@danielkoptak, Ough, an oversight of a simple !

PHP
if (!$this->isAdmin()) {
   return;
}

In that, case I stand corrected, your code does work.

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago