Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

What's the right way to enable a plugin's blueprint?

Started by Julien 7 years ago · 0 replies · 680 views
7 years ago

I'm writing a plugin that comes with some partial blueprints. When I tried to import these in my theme's blueprints, nothing would show up in the admin. A quick search revealed that you first have to put some code in the plugin's php file to enable the blueprint. However, I found two ways of achieving it and both seem to work. They can also be combined and everything still seems to work.

Approach 1 - via getSubScribedEvents():

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

public function onGetPageBlueprints(Event $event)
{
    $types = $event->types;
    $types->scanBlueprints('plugin://' . $this->name . '/blueprints');
}

Approach 2 - via $this->enable:

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

public function onPluginsInitialized()
{
    if ($this->isAdmin()) {
        $this->enable([
            'onGetPageBlueprints' => ['onGetPageBlueprints', 0] 
        ]);
    }
}

public function onGetPageBlueprints(Event $event)
{
    $types = $event->types;
    $types->scanBlueprints('plugin://' . $this->name . '/blueprints');
}
  • What's the difference in these approaches?
  • Which one seems to be the more appropriate for the job?

Suggested topics

Topic Participants Replies Views Activity
General · by Karmalakas, 2 days ago
2 75 24 hours ago
General · by Hanns Mattes, 2 weeks ago
0 165 2 weeks ago
General · by Dima, 3 weeks ago
1 164 3 weeks ago
General · by pamtbaau, 3 weeks ago
0 270 3 weeks ago
General · by lynbor, 2 months ago
5 755 1 month ago