[4.3.5]: Missing asset when overwriting the global set variable
What happened?
Description
Hello! From the global set we moved the same fields to the section as the Neo block. In the template, we override the global variable with a dynamically loaded section and access the asset field and there is no expected data. The other (text) fields contain the data just fine.
- Asset returns the correct data directly from the section.
- Asset from a global variable returns the correct data.
- Asset from a "global" variable will no longer return the correct data after being overwritten by a section. It should return data from the section, but it doesn't.
We don't know if this is a bug or some optimization feature.
We are not sure if this can always be replicated or if it is some edge-case on that particular site that has undergone extensive changes.
Let us to describe it in a very simplified form.
Steps to reproduce
- Create a field
imageFieldof typeasset. - Create a field
textFieldof typetext. - Create a global set
myGlobaland set the created fields to it. - Fill the data in
myGlobal. - Create a
mySectionof typestructure. - (Maybe not essential) Set the
mySectionsection with theimageFieldandtextFieldfields. - (Maybe not essential) Fill the data in
mySection. - Create a new field
neoFieldof typeNeo. - Create a
neoBlockin theneoFieldfield and set theimageFieldandtextFieldfields to it. - (Maybe not essential) Remove the
imageFieldandtextFieldfields frommySection. - Add the
neoFieldfield to themySectionsection. - Fill the
neoFielddata. - In the twig template, get the section into the variable
set myEntry = craft.entries.section('mySection').slug('my-slug').one(). - In the twig template, override the global variable
set myGlobal = myEntry.neoField.one() - In the twig template, access the asset
myGlobal.imageField.one().
Expected behavior
{# global set #}
{{ myGlobal.imageField.one().url }}
-> /lorem/ipsum.jpg
{# overriden global set #}
{% set myEntry = craft.entries.section('mySection').slug('my-slug').one() %}
{% set myNeoBlock = myEntry.neoField.one() %}
{% set myGlobal = myNeoBlock %}
{{ myGlobal.imageField.one().url }}
-> error, calling "url" property over an undefined object
myGlobal.imageField.one() should return the same data as myNeoBlock.imageField.one().
Actual behavior
Asset field returns null or it throws an error when accessing some property.
The data in the variable was overwritten in a weird way. Some of it is fine, some of it is not (especially assets).
Thank you.
Craft CMS version
4.3.5
PHP version
8.1.11
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
- Neo -
3.5.16
Hi, thanks for getting in touch. I'm unable to replicate this behaviour on a fresh installation. Is there anything in the logs (storage/logs/web.log) that would suggest why this is happening?
Hello Iwona,
there's nothing interesting in the logs. Only yii\db\Connection::open, yii\web\Session::open and application with request context are in the clear logs in storage/logs/web.log.
Maybe the structure of the twig templates may also have an influence.
We have index.twig, where the global variable is overwritten:
{# overriden global set #}
{% set myGlobal = myNeoBlock %}
...
{% block content %}{% endblock %}
Then page.twig is used for entry:
{% extends "index.twig" %}
{% block content %}
...
{% include 'subpage.twig' %}
...
{% endblock %}
And that includes subpage.twig:
{% dd myGlobal %} -> myGlobal is instance of benf\neo\elements\Block as expected
{% dd myGlobal.textField %} -> returns data as expected
{% dd myGlobal.imageField %} -> instance of craft\elements\ElementCollection with no items
Thank you.
Thanks! Are you, by any chance using with when getting the entry that contains neoField?
The only way I can get {% dd myGlobal.imageField %} to return an empty instance of craft\elements\ElementCollection is if I get the entry like this:
{% set myEntry = craft.entries.section('mySection').slug('neo-test').with(['neoField.imageField']).one() %}.
To be clear, the right way to eager load relational fields from within the neo field is:
.with(['neoField.neoBlock:imageField']), in which case I get back Illuminate\Support\Collection with an eager loaded asset in it, which I can then access via: {% dd myGlobal.imageField.0 %}.
Not really. It is as described in the "Expected behavior" section - without with.
It doesn't work only for the global variable override and only for the asset. And we have no idea why. If we don't override the global variable but name it uniquely, say myGlobal2, it works fine. That's how we solved it in the end.
It is possible that we are doing something wrong or missing something. We ran into this strange behavior and wanted to report it rather than ignore it.
We searched further after your answer and found the following code in page.twig:
{% do craft.app.elements.eagerLoadElements(className(myGlobal), [myGlobal], ['imageField']) %}
If we remove the row or edit the field name, it returns the asset fine in both cases. So overwriting the global variable works fine.
Thank you for the help!
Great news! I'm glad you got to the bottom of it.