cms icon indicating copy to clipboard operation
cms copied to clipboard

[4.3.5]: Missing asset when overwriting the global set variable

Open licvido opened this issue 3 years ago • 6 comments

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

  1. Create a field imageField of type asset.
  2. Create a field textField of type text.
  3. Create a global set myGlobal and set the created fields to it.
  4. Fill the data in myGlobal.
  5. Create a mySection of type structure.
  6. (Maybe not essential) Set the mySection section with the imageField and textField fields.
  7. (Maybe not essential) Fill the data in mySection.
  8. Create a new field neoField of type Neo.
  9. Create a neoBlock in the neoField field and set the imageField and textField fields to it.
  10. (Maybe not essential) Remove the imageField and textField fields from mySection.
  11. Add the neoField field to the mySection section.
  12. Fill the neoField data.
  13. In the twig template, get the section into the variable set myEntry = craft.entries.section('mySection').slug('my-slug').one().
  14. In the twig template, override the global variable set myGlobal = myEntry.neoField.one()
  15. 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

licvido avatar Jan 27 '23 11:01 licvido

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?

i-just avatar Jan 30 '23 11:01 i-just

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.

licvido avatar Jan 30 '23 12:01 licvido

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 %}.

i-just avatar Jan 30 '23 13:01 i-just

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.

licvido avatar Jan 30 '23 13:01 licvido

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!

licvido avatar Jan 30 '23 15:01 licvido

Great news! I'm glad you got to the bottom of it.

i-just avatar Jan 30 '23 15:01 i-just