-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Calling Craft::$app->globals in a plugins config file causes global fields to return as NULL #5688
Comments
That config file is loaded before Craft is fully initialized, and there are always going to be weird bugs like this if any element queries (such as loading a global set) are executed before Craft is finished initializing. Instead, maybe create a custom module which listens for EVENT_INIT, and sets the Restrict settings from there. use craft\web\Application;
use superbig\restrict\Restrict;
Craft::$app->on(Application::EVENT_INIT, function() {
$global = Craft::$app->globals->getSetByHandle('handle');
$plugin = Restrict::getInstance();
$plugin->settings->settingName = $global->fieldHandle;
// ...
}); |
@brandonkelly Thanks for taking the time to reply and for confirming it's related to the initialisation process, Is it a general recommendation to avoid calling Craft::$app in config files due to potential issues like this? I'll look at using a module to set these settings instead. Thanks again! |
Mainly just make sure you’re avoiding element queries there, since those rely on the application being fully initialized. |
Good to know, thank you! |
Description
I'm having a weird issue under Craft 3 where if I call
Craft::$app->globals->getSetByHandle('handle')
in a plugins config file, in this case: https://github.com/sjelfull/craft3-restrictconfig/restrict.php
. When trying to return the value of a global field in from another set in an unrelated component i.e. a module, the value is always NULL even though it's a populated and valid field. It seems to be happening on PlainText fields, entry and category fields still seem to work fine, it's possible happening with other field types too.I didn't think calling
Craft::$app->globals->getSetByHandle()
in a config file would be a problem, but it seems to cause issues.To debug, I created a copy of
index.php
and called itdebug.php
and simply removed the last line$app->run();
line so I can execute Craft:: without the full app being run and I can see the NULL behaviour happening. If I disable the restrict plugin or simply remove the Craft::$app->globals call in the config file, the global field values are all returned correctly.I'm not sure if this is a Craft bug or related to this specific plugin, I have found information eluding to a potential loading/initialisation problem previously, which seemed to be a bit of both scenario.
I have also logged the issue with the plugin developer directly:
Steps to reproduce
config/restrict.php
. It would appear, this doesn't need to be assigned into the config itself for this issue to occur.Additional info
The text was updated successfully, but these errors were encountered: