Hey everyone,
I’m currently storing all my hooks in a seperate file with this line in the config:
'hooks' => require_once 'hooks.php'
To hide some Elements in the panel for specific user roles i started using this modified blueprint path.
My new index file looks like this:
<?php
require 'kirby/bootstrap.php';
$kirby = new Kirby();
$user = $kirby->user();
if ($user && $user->role() == 'admin') {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/admin',
],
]);
} elseif ($user && $user->role() == 'leadeditor') {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/leadeditor',
],
]);
} else {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/editor',
],
]);
};
echo $kirby->render();
Since I started using this method, I get the following error message and it only disappears when I comment out the hook line or reset the config file to it’s basic set up. Anyway the code mentioned above did work.
I tried the same with an empty hook file, but the error didn’t change. To keep things cleaned up, I would be very grateful, if you can help me out with this!
Kirby Version: 3.3.5
PHP: 7.4.1
Does you hooks.php return an array?
<?php
return [
// hooks here
];
Yes, I tried with this empty hook, but no changes.
Could you share your /site/config/config.php and /site/config/hooks.php please?
Of course!
config.php:
<?php
//set time zone correctly
date_default_timezone_set('Europe/Amsterdam');
//loads env plugin into kirby instance
(new \Beebmx\KirbyEnv('./'))->load();
return [
// environment variable "env" plugin
'debug' => env('KIRBY_DEBUG', false),
// xml sitemap options
'omz13.xmlsitemap.cacheTTL' => 30,
'omz13.xmlsitemap.includeUnlistedWhenSlugIs' => [ 'team', 'impressum', 'datenschutzerklaerung'],
'omz13.xmlsitemap.includeUnlistedWhenTemplateIs' => [ ],
'omz13.xmlsitemap.excludePageWhenTemplateIs' => [ 'error' ],
'omz13.xmlsitemap.excludePageWhenSlugIs' => [ 'error' ],
'omz13.xmlsitemap.excludeChildrenWhenTemplateIs' => [ 'error' ],
// Change home page to 'start'
'home' => 'start',
// Limit picture width (px) at upload
'medienbaecker.autoresize.maxWidth' => 2000,
//activate poor mans cron for autopublish plugin
'bvdputte.kirbyAutopublish.poormanscron' => true,
'bvdputte.kirbyAutopublish.poormanscron.interval' => 5,
// //loads hooks into config file
'hooks' => require_once 'hooks.php'
];
hooks.php:
<?php
return [
];
Here’s also a list of plugins that are installed:
Autoresize
Editor
K3-Date-Format
Kirby-Autopublish
Kirby-builder
Kirby-env
Kirby-helpsection
Kirby-log
Kirby-twig
Kirby3-xmlsitemap
pagetable
and our own plugin:
<?php
Kirby::plugin('formatplus/formatplus', [
// creates virtual user pages
'routes' => [
[
'pattern' => 'autoren/(:any)',
'action' => function ($value) {
$data = [
'currentUser' => urldecode($value),
];
return page('autoren')->render($data);
}
]
],
]);
And your hooks.php is in the same folder as the config.php?
Yes, both are in the site/config folder. It even did work until I changed the blueprint path in the index.php file, which is located in the root folder.
Hm
, that’s a bit weird.
Do all blueprint folders contain the default.yml blueprint and a site.yml?
Have you checked if any plugins could possibly interfere?
I just checked it, and it seems to be. Here’s the folder structure, which is exactly the same in the leadeditor and the editor folder.
Alright I will uninstall all the plugins and install them one by one to check if they are the cause. I’ll keep you in the loop.
I uninstalled every plugin except for kirby-env to get the error message.
Which has changed and I’m not sure what it means.
According to the error message, there is no /site/templates/default.php template which is required.
Indeed, was kinda my mistake. I forgot I had some twig code in there and I uninstalled the twig plugin, which most likely caused this error.
Anyways the plugins are uninstalled and the plugins env and twig are still installed to keep everything working and I doubt that one of these can cause the hook error, that is still coming up.
The config.php file:
<?php
(new \Beebmx\KirbyEnv('./'))->load();
return [
'debug' => env('KIRBY_DEBUG', false),
'home' => 'start',
'hooks' => require_once 'hooks.php',
];
The hooks.php file:
<?php
return [
];
The index.php file:
<?php
require 'kirby/bootstrap.php';
$kirby = new Kirby();
$user = $kirby->user();
if ($user && $user->role() == 'admin') {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/admin',
],
]);
} elseif ($user && $user->role() == 'leadeditor') {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/leadeditor',
],
]);
} else {
$kirby = new Kirby([
'roots' => [
'blueprints' => __DIR__ . '/site/blueprints/editor',
],
]);
};
echo $kirby->render();
Just to clarify: So your setup works when either
- the blueprints are in their normal folder and you define the hooks in a separate file or
- you set up the blueprints like above, but keep the hooks in your config
Is that correct?
And could you provide your project or a simplified version of it for some testing?
Yes, exactly! I just downloaded the starterkit with example content and could reproduce the error. That’s probably the most simplified version.
You can download the setup from this repository: https://gitlab.com/tobias.michaely/hook-error
The problem is require_once. If you change it to 'hooks' => require 'hooks.php', it will work.
Thanks for providing the example!
I think this is because the Kirby object is redefined in index.php depending on user role.
You’re right it works now, thank you so much helping me out. Not sure, but maybe you should add a comment about this behavior in your docs?
Well, this whole setup in index.php with redefining Kirby for different user roles is more a hack than anything we promote officially in the docs. So I wouldn’t really know where to put that…