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

Get page object for modular page from PHP

Started by Julien 5 years ago · 3 replies · 863 views
5 years ago

I'm working on a plugin that dynamically adds some form inputs and form data to existing forms. These forms could also be on modular pages. I need to read the header data (frontmatter) of the pages that include the relevant form (or forms). Doing it this way works if the form is included in a regular page:

PHP
public function onFormInitialized(Event $event) {
    $page = $this->grav['page'];
    $header = $page->header();
    /* ... */
}

However, if the form is included in a modular page, then the first line, $this->grav['page'] will cause the entire page to display a 404.

What's the right way to achieve this that will work for any type of page?

5 years ago

I tried to get the page from the form itself:

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

But this leads to:

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

So the search continues...

5 years ago

@domsson, Bit of playing with the debugger....

Let say we have a modular with a form as child module:

TXT
user/pages
├── 01.home
├── ...
└── 04.modularform
    ├── _contact
    │   └── form.md
    └── modular.md

Then in our plugin we can do the following to get the header from both the modular and its child form module.

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

    /** @var Page */
    $modular = $form->getPage();
    $modularHeader = $modular->header();

    /** @var Collection */
    $collection = $modular->collection();

    /** @var Page */
    $contact = $collection->filter(fn ($module) => $module['slug'] === '_contact')->first();
    $contactHeader = $contact->header();

    ...
}

Update 2021-04-12:
On revisit of this code in a newer post, it seems that my suggested code does indeed not work in all circumstances. It only works when the page has already been cached. If not yet cached, the page is not yet available during onFormInitialized.

👍 1
last edited 04/12/21 by pamtbaau
5 years ago

I'm impressed by your knowledge of Grav and your dedication to help, pamtbaau. Thank you very much. Using the $form->getPage() approach is exactly what I tried today, but having the line in there would lead to an error, as outlined in my reply above. I take it you tried your code, however, and it worked for you?

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 44 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