-
Notifications
You must be signed in to change notification settings - Fork 641
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
currentUser contentBehavior empty if a plugin calls "getAllFields" #4944
Comments
I think this is a plugin issue; plugins are loaded one by one during the bootstrap process, so plugins shouldn’t assume that the app is fully bootstrapped yet from their In hindsight, element queries should probably be ensuring that the app is fully bootstrapped before executing, and throw an exception if not, to ensure this sort of thing isn’t possible. Too late for Craft 3 now, but I will go ahead and mark this as an enhancement for Craft 4. |
Great investigation/writeup, though, Robin @Anubarak . And I have had to use EVENT_AFTER_LOAD_PLUGINS myself to avoid the area of problems. Best result is that Brandon's going to put in a guarantee. |
Let’s keep this open so it doesn’t get overlooked when we’re working on v4 :) |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Description
This is going to be a little bit tricky and it took me a while to fully understand why it happens and I still don't really know how to prevent it.
I'm not really sure if this is an issue on your end (because it's technically only caused by plugins (such as Relabel or CpFieldInspect))
When you call
Craft::$app->getUser()->getIdentity()
in a pluginsinit
method or in thePlugins::EVENT_AFTER_LOAD_PLUGINS
event strange things happen in certain scenarios. I already created an issue for that in CpFieldInspect but I'll explain the same thing a little bit more detailed again.When you call
Craft::$app->getUser()->getIdentity()
it will create a newElementQuery
to fetch the user (if it's the first time)The ElementQuery will then call
Craft::$app->getFields()->getAllFields()
because it needs to select the correct columns for the users content.Craft::$app->getFields()->getAllFields()
will then create a Query to load all the fields and now comes the tricky partComponentHelper::createComponent
will then try to create the other plugin and if another plugin then callsCraft::$app->getFields()->getAllFields()
orCraft::$app->getUser()->getIdentity()
again the function will only return the fields that are currently created.So for example: if you have a plugin that has a custom field type and you call this fields name/handle
a
it might be the very first field that is fetched.When Craft fetches that field it will load all plugins during that very first process but
Fields::_fields
only contains that one single field. So When a plugin during that whole process callsgetAllFields
or something that callsgetAllFields
like everyElementQuery
it returns that "cached" result instead of all fields.In my case I had a custom field called
accessToken
which leaves mycurrentUsers
fields totally empty becauseSteps to reproduce
aaaa
ElementsQuery
in the same or another pluginsinit
function or in yourPlugins::EVENT_AFTER_LOAD_PLUGINS
eventgetAllFields
Solution to make it better
As I said, I don't know how to solve it entirely, but a solution to make it at least a little bit better would be to implement a
getAllFieldHandlesInContentTable
(Or something shorter) so at least Elements will be propagated correctlyI'm not good in explaining, I hope you get the point. Maybe it's easier with a stack trace...
Edit:
my simple question is: is this an issue of Plugin developers that try to grab the Users Identity in their plugins
init
function to add certain events or something (so faulty usage of Craft) or an unexpected behavior?The text was updated successfully, but these errors were encountered: