craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.2k stars 617 forks source link

[5.x]: Aliasing of fields in field layout doesn't work #15225

Closed lexisother closed 1 week ago

lexisother commented 2 weeks ago

What happened?

Description

I am trying to access one of the fields on my entry type using an alias local to that entry type. The field's actual handle is innovatieImage, and the handle defined on the entry type is blockImage. When accessing the field on my entry as entry.blockImage, I get an empty craft\elements\ElementCollection. However, when I remove the field's alias on the entry type (so from blockImage to empty, which defaults to innovatieImage), I get a filled craft\elements\db\AssetQuery on which I can properly call .one() to retrieve my image.

Steps to reproduce

  1. Create two fields of type Image, one called blockImage and the other called testImage
  2. Add testImage to an entry type's field layout, and alias it to blockImage
  3. Create an entry and add an image to the field we added
  4. Fetch the entry in Twig and try to access blockImage on it

Expected behavior

The alias works and I correctly get my image.

Actual behavior

I get an empty ElementCollection.

Craft CMS version

5.2.1

PHP version

8.2.19

Operating system and version

Linux 6.9.3

Database type and version

MariaDB 10.3.39

Image driver and version

Imagick 3.7.0

Installed plugins and versions

No response

i-just commented 2 weeks ago

Hi, thanks for getting in touch! I can’t seem to replicate this. Following your steps, I’m able to get the entry and then the content of the blockImage Assets field (where blockImage is the overwritten handle) on that entry.

Could you please share your twig code to get the entry and the content of the blockImage?

lexisother commented 1 week ago

It's a bit difficult to share my Twig code concisely, as it's using a custom matrix block template including system. The general gist of it is as follows:

index.twig where entry.blocks is a matrix field with all my entry types:

{% include "_partials/blocks.twig" with { 'blocks': entry.blocks } %}

_partials/blocks.twig just loops over the blocks and includes _partials/blocks/{block.type}.twig with block passed into it.

Where I was encountering the issue was on a matrix block with a field of type Assets where the field handle was blockImage2, locally aliased to blockImage in that matrix field. I then tried using block.blockImage.one() to get my image, which is how I ran into the bug I described. Only after renaming blockImage2 to imageTextImage and removing the alias for it did I get the expected result.

I understand the issue is super hazy and probably sounds like a one-off, but this was happening in more places than just my Assets block. I had to give all my matrix blocks unique handles and remove the aliases for them to fix it.

i-just commented 1 week ago

Thanks for sharing more details on this!

Unfortunately, I’m unable to replicate this even after following your template setup.

blockImage2 suggests that maybe you had the same Assets field added to this particular field layout more than once. Would you happen to know if that was the case? It shouldn’t be possible as relational fields are not multi-instance, but a bug allowed this in the early beta version (fixed in 5.0.0-beta.2). Or maybe the field in question was of a different type initially?

It sounds like you have already changed your setup. Do you have a backup (project config or database) of the setup with this problem? If so, could you send it to support@craftcms.com along with composer.json and composer.lock and point out the entry where you saw this happen so we can try to replicate and dig deeper?

lexisother commented 1 week ago

Now that I reverted my workspace to when I still had the issue, I seem to be unable to reproduce this strange bug... Super unfortunate, because I was hoping this wasn't just a strange one-off. Sorry for bothering!

i-just commented 1 week ago

Thank you for checking, and I’m glad to hear it’s working as expected! Let us know if this happens again in a reproducible way.