Skip to content

[4.x]: merging arrays via twig filter drops custom fields #13009

Closed
@therealpecus

Description

@therealpecus

What happened?

Description

When merging the results of an element query in a twig template with some data, custom fields are lost. The issue was originally identified with properties that were returned as collections (specifically, eager loaded elements).

Code that is migrated from Craft 3.x might suffer from this bug, and it might be quite puzzling since it's not documented in the migration path.

We use the merge pattern a lot in the main view templates to keep components as pure as possible (no Craft specific Twig extensions).

I believe collection objects passed to merge, map and reduce twig filters should be recursively converted to arrays in order to preserve the old behavior and this should be documented. Alternatively, these filters should be updated to preserve collections.

Steps to reproduce

  1. do an entry query with an eager loaded field {% set allNews = craft.entries().section('sectionNews').highlight('1').with([['thumbnailNews', 'tags']).one() %}
  2. merge some data {% set cards = allNews|merge({ extraField: 'demo' }) %}

Expected behavior

cards contains all fields from the query, specifically thumbnailNews and tags

Actual behavior

fields that were returned as collections are missing

Explanation

Craft CMS version

4.4.5

PHP version

8.1.x

Operating system and version

Linux (ddev)

Database type and version

n/a

Image driver and version

n/a

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions