We’ve got a plugin to import entries from an old site. This plugin provides some snippets to display import logs after running an import and importers are executed by a custom route.
As soon as we use kirby()
inside our route, the provided snippets can no longer be found. So we tried wrapping our routes in a function as mentioned in the docs but the result is the same: the snippets from the plugin are not found. Any other snippet provided via /site/snippets
is accessible though.
How can we use the $kirby
object while still being able to access our snippets?
This is the code of our plugin:
<?php
include_once __DIR__ . '/Importer.php';
Kirby::plugin('hananils/kirby-importer', [
'snippets' => [
'hananils/importer/header' => __DIR__ . '/snippets/header.php',
'hananils/importer/footer' => __DIR__ . '/snippets/footer.php',
'hananils/importer/list' => __DIR__ . '/snippets/list.php',
'hananils/importer/run' => __DIR__ . '/snippets/run.php'
],
'routes' => function ($kirby) {
return [
[
'pattern' => 'import',
'action' => function () {
snippet('hananils/importer/list');
}
],
[
'pattern' => 'import/(:all)',
'action' => function ($importers) use ($kirby) {
$importers = explode('+', $importers);
$logs = [];
foreach ($importers as $importer) {
$file =
$kirby->root('site') .
'/importers/' .
$importer .
'.php';
if (F::exists($file)) {
include_once $file;
$name = Str::ucfirst($importer);
$class = new $name();
$logs[] = $class->run();
}
}
snippet('hananils/importer/run', [
'logs' => $logs
]);
}
]
];
}
]);
$kirby->root('site')
in the second route is the problematic line. If I remove it the snippet is found again but of course my importer doesn’t run because the URL is no longer complete.