craftcms / craftcms/cms

[5.x]: Matrix field containing an Entries field with the same handle as the matrix, always returns an empty array

Open
#16,153 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Description

With Craft 5 we now have the possibility to change the handle of a field, which means that the handle of a Matrix field might be the same as one of the fields of the entries it contains. When doing so with an Entries field within a Matrix, the Matrix always returns an empty array.

Note that there are also issues when the field inside the Matrix is not an Entries type. If I try with a Plain Text field for instance, Craft will return an internal error. However, Entries seems like the weirdest case, since I'm getting no errors, only an empty array.

Steps to reproduce

I'll focus on the case with the Entries type.

  1. Create a "sectionEntries" field with type set to "Entries".
  2. Create a "section" entry type which contains the "sectionEntries" field.
  3. Create a "section" Matrix field which maps to the "section" entry type.
  4. Add the field to a page/entry, and rename the handle to "sectionEntries".
  5. The top-level "sectionEntries" Matrix field always returns an empty array.
Expected behavior

I would expect either to have an error indicating that we shouldn't nest fields with the same handles, or for nested handles to be supported.

In the case of a Plain Text field (instead of an Entries field): Craft does return an error, but I believe the message could be clearer. Here's what Craft returns:

"debugMessage": "craft\\helpers\\StringHelper::shortcodesToEmoji(): Argument #1 ($str) must be of type string, array given, called in *** on line 168",
"message": "Internal server error",
"extensions": {
    "category": "internal"
},
Actual behavior

In the case of an Entries field within a Matrix field, the Matrix field always returns an empty array.

In the case of a Plain Text field within a Matrix field, Craft returns a bit of a cryptic error.

Thank you!

Craft CMS version

5.5.2

PHP version

8.3.13

Operating system and version

Linux 4.18.0-553.16.1.el8_10.x86_64

Database type and version

MySQL 8.0.40

Image driver and version

GD 8.3.13

Installed plugins and versions

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named. Start by reproducing the described Craft 5.5.2 setup with a Matrix field and nested Entries field sharing a handle, then trace the Matrix and nested-field handling. Done means the same-handle case is either supported or rejected clearly, and the Plain Text case no longer exposes the cryptic internal error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.