onTwigPageVariables event handler and modular pages

For a page with _modular pages, the onTwigPageVariables is triggered once for the main page and each time for any of the modular pages. How can one figure out within the onTwigPageVariables event handler which page the call relates to? $this->grav['page']; always returns the main page even when called for the modular pages.

Reason I am asking this is that I like to process a modular page’s header from within onTwigPageVariables event handler.

the Twig variable page should be associated with the actual page even in modular pages. Just do a:

{{ dump(page.title) }}

in the Twig of each modular page type and the modular surrounding page, should display the appropriate name.

Yes, that work on template level. But I like to check a modular header from a plugin.
I have the feeling the onTwigPageVariables has to change and the page object needs to be passed down.

Like in this patch:

    {
        $content = $content !== null ? $content : $item->content();

        // override the twig header vars for local resolution
        $this->grav->fireEvent('onTwigPageVariables',  new Event(['page' => $item]));

And then the handler can check like this:

    public function onTwigPageVariables(Event $event)
    {
        $page = $event['page'];
        $header = $page->header(); 

Interesting, but this would cause significant backwards compatibility problems for all plugins/themes that have this event method defined without the Event $event in the method signature.

No it wouldn’t as the passed event argument is then simply ignored.

@rhukster @hwmaier Is right. You can look at the Symphony EventDispatcher at line 160. For convenience I will post a snippet here:

protected function doDispatch($listeners, $eventName, Event $event)
{
  foreach ($listeners as $listener) {
    call_user_func($listener, $event, $eventName, $this);
    if ($event->isPropagationStopped()) {
      break;
    }
  }
}

As you can see, besides the event, the event name and the dispatcher itself is passed to the function. @hwmaier’s idea to pass the page to the onTwigPageVariables is a good point without breaking any backward-compatibility. :wink:

I submitted a PR for the described patch, see https://github.com/getgrav/grav/pull/386

Yup you are correct! I didn’t dig deep into how the dispatch handled the event. I’ll take a look at the PR. Thanks!