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 :
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 !
I find a solution.
The template require a file with the same name in the plugin blueprints folder
@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
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');
}
}