---
title: 'How get all pages in plugin for admin'
url: 'https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845'
markdown: 'https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845.md'
date: '2026-06-21'
description: "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"
---

[Plugins](https://getgrav.org/forum/plugins)

#   How get all pages in plugin for admin 

 [plugin](https://getgrav.org/forum/tag/plugin) [plugins](https://getgrav.org/forum/tag/plugins)

 Solved by Daniel [View solution](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32822)

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

 [5 years ago](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32816 "Permalink") 

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](https://cdn.getgrav.org/forum/files/92/92fc99ee0d7351313bbad9ae0eb56d08ad27d1c149166bbccfdcca2fbc15822f.png)

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

 [5 years ago](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32817 "Permalink") 

@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](https://learn.getgrav.org/17/plugins/grav-lifecycle):

> 1. Fire **onPagesInitialized** event with \[pages\]

```php
public function onPagesInitialized($event): void
{
    /** @var Pages */
    $pages = $event['pages'];
}
```

 [5 years ago](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32822 "Permalink")  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](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32823 "Permalink") 

@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](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32824 "Permalink") 

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](https://getgrav.org/forum/plugins/how-get-all-pages-in-plugin-for-admin-t5845#post-32825 "Permalink") 

@danielkoptak, Ough, an oversight of a simple `!`

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

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

[Log in](https://getgrav.org/login?return=%2Fforum%2Fplugins%2Fhow-get-all-pages-in-plugin-for-admin-t5845) to reply.

###  Suggested topics

 | Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| [Conflict with updated Form plugin and Add Page By Form](https://getgrav.org/forum/plugins/conflict-with-updated-form-plugin-and-add-page-by-form-t9480)    [Plugins](https://getgrav.org/forum/plugins) · by DrPen, 3 weeks ago |  | 2 | 280 | [3 weeks ago](https://getgrav.org/forum/plugins/conflict-with-updated-form-plugin-and-add-page-by-form-t9480 "by DrPen") |
| [grav-mcp update\_page sends lang in the body instead of the query string, so non-default languages are never updated](https://getgrav.org/forum/plugins/grav-mcp-update-page-sends-lang-in-the-body-instead-of-the-query-string-so-non-default-languages-are-never-updated-t9469)    [Plugins](https://getgrav.org/forum/plugins) · by renedebaaij, 4 weeks ago |  | 1 | 260 | [3 weeks ago](https://getgrav.org/forum/plugins/grav-mcp-update-page-sends-lang-in-the-body-instead-of-the-query-string-so-non-default-languages-are-never-updated-t9469 "by Andy Miller") |
| [Tools / Scheduler some jobs not running.](https://getgrav.org/forum/plugins/tools-scheduler-some-jobs-not-running-t9471)    [Plugins](https://getgrav.org/forum/plugins) · by Thomas, 4 weeks ago |  | 1 | 274 | [4 weeks ago](https://getgrav.org/forum/plugins/tools-scheduler-some-jobs-not-running-t9471 "by Thomas") |
| [How to clone a flex object in twig](https://getgrav.org/forum/plugins/how-to-clone-a-flex-object-in-twig-t9438)    [Plugins](https://getgrav.org/forum/plugins) · by complanar, 1 month ago |  | 6 | 592 | [1 month ago](https://getgrav.org/forum/plugins/how-to-clone-a-flex-object-in-twig-t9438 "by complanar") |
| [Integration of GRAV articles into my principal website](https://getgrav.org/forum/plugins/integration-of-grav-articles-into-my-principal-website-t9440)    [Plugins](https://getgrav.org/forum/plugins) · by BenLaKnet, 1 month ago |  | 1 | 495 | [1 month ago](https://getgrav.org/forum/plugins/integration-of-grav-articles-into-my-principal-website-t9440 "by BenLaKnet") |
