How get all pages in plugin for admin

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:

    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:

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

@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]
public function onPagesInitialized($event): void
{
    /** @var Pages */
    $pages = $event['pages'];
}

Ok, my fault, for me works:

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

@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.

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.

all my code looks like this:

<?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

@danielkoptak, Ough, an oversight of a simple !

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

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