---
title: 'Form plugin returns null instead of a page via getPage()'
url: 'https://getgrav.org/forum/plugins/form-plugin-returns-null-instead-of-a-page-via-getpage-t8711'
markdown: 'https://getgrav.org/forum/plugins/form-plugin-returns-null-instead-of-a-page-via-getpage-t8711.md'
date: '2026-06-21'
description: 'From a plugin, I want to find the page associated with the forms found on modular subpages. This should work, as far as I understand: public function'
---

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

#  Form plugin returns null instead of a page via getPage() 

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

  Started by Julien 6 years ago · 2 replies · 1356 views    

 [6 years ago](https://getgrav.org/forum/plugins/form-plugin-returns-null-instead-of-a-page-via-getpage-t8711#post-32328 "Permalink") 

From a plugin, I want to find the [page associated with the forms found on modular subpages](https://getgrav.org/forum/plugins/get-page-object-for-modular-page-from-php-t4689). This *should* work, as far as I understand:

```php
public function onFormInitialized(Event $event) {
    $form = $event['form'];
    $page = $form->getPage();
    /* ... */
}
```

However, this leads to:

```
Return value of Grav\Plugin\Form\Form::getPage() must implement interface Grav\Common\Page\Interfaces\PageInterface, null returned
```

I was digging through the debugger as well as the documentation and source code, but can't figure out why I'm seeing this error. Any hints appreciated.

 [6 years ago](https://getgrav.org/forum/plugins/form-plugin-returns-null-instead-of-a-page-via-getpage-t8711#post-32329 "Permalink") 

@domsson, The following seems to be happening...

`Form::getPage()` should either return a page or null. However, `Form::getPage()` is being defined as returning a `PageInterface` and will therefor throw an Error if a null value is returned. It should probably be defined as returning a nullable value: `?PageInterface`.

Why my example code fails in your test and succeeded in my test?

1. If the page has not yet been cached (it's new, or has been changed), `onFormInitialized` is handled **before** `onPageInitialized`.
    `Form::getPage()` should then return `null`, however, Grav decides to throw an error. I think this is a bug...
2. If the page has been cached already, then event `onFormInitialized` is being handled **after** `onPageInitialized`.
    `Form::getPage()` will then correctly return the page.

That explains, I guess, why my test succeeded and yours fails. I probably did my test with an already cached page... I'll update the previous post.

Alternative:

- Add class variable `$page` and assign it in `onPageInitialized`.
- In `onFormInitialized` check if `$page` has been set and use the value.

But isn't `onFormInitialized` being called before `onPageInitialized` when page is not yet cached?
Yes.. However, it seems that if the page has not yet been cached, `onFormInitialized` is called again after page is cached and `onPageInitialized` has been called.

Hope this helps...

 👍 1 

last edited 04/13/21 by pamtbaau

 [6 years ago](https://getgrav.org/forum/plugins/form-plugin-returns-null-instead-of-a-page-via-getpage-t8711#post-32330 "Permalink") 

@domsson, I've Added an [issue](https://github.com/getgrav/grav-plugin-form/issues/518) at the repo of the Form plugin. According the team, it is by design and won't be fixed.

UPDATE: Team is considering how adding extra fields can be done safely.

There are two alternatives to work around this, both using the fact that `onFormInitialized` is called again after a page gets initialised.

Alternative 1:

- Assign the page to class property `$page` during `onPageInitialized`
- Use the class property in `onFormInitialized`

Alternative 2:

- Use try-catch in `onFormInitialized`

    ```php
    public function onFormInitialized(Event $event) {
    /** @var Form */
    $form = $event['form'];

    try {
        /** @var Page */
        $page = $form->getPage();
        // Add extra fields to form
    } catch (\TypeError $error) {
        // Handle or ignore
    }
    }
    ```

**Note**: The error thrown will change from `\TypeError` to `LogicException` in the near future. Keep an eye on the issue mentioned above.

last edited 04/16/21 by pamtbaau

[Log in](https://getgrav.org/login?return=%2Fforum%2Fplugins%2Fform-plugin-returns-null-instead-of-a-page-via-getpage-t8711) 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") |
