Skip to content
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

Closed
jamesmacwhite opened this issue Feb 20, 2020 · 4 comments

Comments

@jamesmacwhite
Copy link
Contributor

jamesmacwhite commented Feb 20, 2020

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-restrict config/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 it debug.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

  1. Install restrict CP plugin
  2. Call Craft::$app->globals->getSetByHandle('handle') in config/restrict.php. It would appear, this doesn't need to be assigned into the config itself for this issue to occur.
  3. Try and get a global field that is plaintext in another part of Craft, the returned output will return NULL, unless the restrict plugin is disabled or the Craft::$app->globals call is removed from the config file.

Additional info

  • Craft version: 3.3.20.1
  • PHP version: 7.2
  • Database driver & version: MySQL 5.6
  • Plugins & versions:
@jamesmacwhite jamesmacwhite changed the title Calling Craft::$app->globals in a plugins config file causes global fields to return NULL Calling Craft::$app->globals in a plugins config file causes global fields to return as NULL Feb 20, 2020
@brandonkelly
Copy link
Member

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;
    // ...
});

@jamesmacwhite
Copy link
Contributor Author

@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!

@brandonkelly
Copy link
Member

Mainly just make sure you’re avoiding element queries there, since those rely on the application being fully initialized.

@jamesmacwhite
Copy link
Contributor Author

Good to know, thank you!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants