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

Event propagation

Started by Muut Archive 11 years ago · 3 replies · 278 views
11 years ago

Are further subscribed events propagated to a plugin even if $this->active is set to false in onPageInitialized()?

PHP
Basically could plugin classes look like this

    public static function getSubscribedEvents()
    {
        return [
            'onPageInitialized' => ['onPageInitialized', 0],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
        ];
    }

    public function onPageInitialized()
    {
        if ($this->isAdmin() ) {
            $this->active = false;
            return;
        }
    } 

or better like this

PHP
    public static function getSubscribedEvents()
    {
        return [
            'onPageInitialized' => ['onPageInitialized', 0],
        ];
    }

    public function onPageInitialized()
    {
        if ($this->isAdmin() ) {
            $this->active = false;
            return;
        }
        $this->enable([
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
        ]);
    }
11 years ago

In short yes. $this->active is not used internally, it is there to be used by your plugin if you need it.

11 years ago

Thanks for clarification. Btw, in above examples I was meant to use onPluginsInitialized instead of onPageInitialized.

11 years ago

It really is the same, you just need to sure you don't try to enable an event that has already occurred. You can use the Grav Lifecycle to reference the order events are fired. Also the Event Hooks page has relevant information.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1344 9 years ago
Archive · by Muut Archive, 9 years ago
2 930 9 years ago
Archive · by Muut Archive, 9 years ago
2 4058 9 years ago
Archive · by Muut Archive, 9 years ago
1 2943 9 years ago
Archive · by Muut Archive, 9 years ago
3 1116 9 years ago