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

Adding own templates from a plugin

Started by Xavier 4 weeks ago · 2 replies · 53 views
4 weeks ago

Hello

I'm looking for a way to see my template in the admin page.
A try many solution without success since 3 days.

In the plugin :

PHP
    public function onPluginsInitialized(): void
    {
        spl_autoload_register(function (string $class): void {
            $prefix = 'Grav\\Plugin\\Cooking\\';
            if (strpos($class, $prefix) !== 0) {
                return;
            }
            $relative = str_replace('\\', '/', substr($class, strlen($prefix)));
            $file     = __DIR__ . '/classes/' . $relative . '.php';
            if (file_exists($file)) {
                require_once $file;
            }
        });

        if ($this->isAdmin()) {
            return;
        }

        $this->enable([
            'onPagesInitialized'  => ['onInjectVirtualPages', 0],
            'onPageInitialized'   => ['onArtefactsRoute', 0],
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
            'onAssetsInitialized' => ['onAssetsInitialized', 0],
        ]);
    }
    public function onGetPageTemplates(Event $event): void
    {
        $types = $event['types'];
        $types['cooking-category'] = 'Cooking - Catégorie';
        $types['cooking-document'] = 'Cooking - Document';
        $types['cooking']          = 'Cooking - Page racine';
        $event['types'] = $types;
    }

But in admin/edit page, we can't find and select them.
The plugin is on.
Cache is cleared.

Merci d'avance !

4 weeks ago

I find a solution.

The template require a file with the same name in the plugin blueprints folder

  • blueprints
    • templatename.yaml
  • templates
    • templatename.html.twig
last edited 05/28/26 by Xavier
4 weeks ago

@Xavier11, Your issue might be "solved" using your suggestion, but looking at the code you presented, I would like to make some suggestions.

To add Page blueprints and custom Twig templates, your plugin's PHP could look as follows when using DevTools to generate the plugin:

PHP
<?php
namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;

/**
 * Class MyPluginPlugin
 * @package Grav\Plugin
 */
class MyPluginPlugin extends Plugin
{
    /**
     * @return array
     *
     * The getSubscribedEvents() gives the core a list of events
     *     that the plugin wants to listen to. The key of each
     *     array section is the event that the plugin listens to
     *     and the value (in the form of an array) contains the
     *     callable (or function) as well as the priority. The
     *     higher the number the higher the priority.
     */
    public static function getSubscribedEvents(): array
    {
        return [
            'onPluginsInitialized' => [
                // Uncomment following line when plugin requires Grav < 1.7
                // ['autoload', 100000],
                ['onPluginsInitialized', 0]
            ],
            'onGetPageBlueprints' => ['onGetPageBlueprints', 0],
        ];
    }

    /**
     * Composer autoload
     *
     * @return ClassLoader
     */
    public function autoload(): ClassLoader
    {
        return require __DIR__ . '/vendor/autoload.php';
    }

    /**
     * Initialize the plugin
     */
    public function onPluginsInitialized(): void
    {
        // Don't proceed if we are in the admin plugin
        if ($this->isAdmin()) {
            return;
        }

        // Enable the main events we are interested in
        $this->enable([
            // Put your main events here
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
        ]);
    }

    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }

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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 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 72 2 months ago
Plugins · by Anna, 3 months ago
5 73 3 months ago