Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

onTwigPageVariables event handler and modular pages

Started by Muut Archive 11 years ago · 10 replies · 317 views
11 years ago

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.

11 years ago

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

11 years ago

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

TWIG
{{ dump(page.title) }}

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

11 years ago

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.

11 years ago

Like in this patch:

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

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

And then the handler can check like this:

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

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.

11 years ago

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

11 years ago

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

PHP
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. ;-)

11 years ago

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!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1345 9 years ago
Archive · by Muut Archive, 9 years ago
2 932 9 years ago
Archive · by Muut Archive, 9 years ago
2 4059 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago