Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: The spread operator still doesn't work correctly in some situations #14827

Closed
MoritzLost opened this issue Apr 17, 2024 · 5 comments
Closed
Assignees

Comments

@MoritzLost
Copy link
Contributor

What happened?

Description

Follow-up to #14783

I've updated to the latest version, and the spread operator now works correctly in most situations. However, the issue still occurs if the object being spread into another object comes from a method call on an object.

This one is unrelated to the preloadSingles config.

Steps to reproduce

Minimal contrived example:

{% do entry.addError('foo', 'bar') %}
{% do entry.addError('alice', 'bob') %}

{% dd {
    ...entry.getErrors(),
    three: ['four'],
} %}

This should result in a flat array with keys foo, alice and three, but I'm getting a nested array instead:

Screenshot 2024-04-17 at 17 18 13

Not sure if there are still more conditions where the spread operator doesn't work correctly. Might need some exhaustive testing. Maybe check with function calls and filters as well.

Craft CMS version

4.8.9

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@brandonkelly
Copy link
Member

That’s something you’ll need to report on the Twig repo.

@MoritzLost
Copy link
Contributor Author

MoritzLost commented Apr 22, 2024

@brandonkelly I don't think this is a Twig issue. I've tried to replicate the problem on Twig Fiddle. This here works fine: https://twigfiddle.com/ovw4ri

This outputs the expected result:

one: two
baz: bat

However, if I put this in a Twig template in my Craft project:

{% set key = { "nested": { one: "two" }} %}

{% set merged = {
    ...attribute(key, 'nested'),
    baz: 'bat',
} %}

{% dd merged %}

I get an incorrect result again:

Screenshot 2024-04-22 at 09 56 54

So it's probably something that Craft is doing, or am I missing something?

Update: This is the compiled template:

        // line 3
        $context["key"] = ["nested" => ["one" => "two"]];
        // line 4
        echo "
";
        // line 5
        $context["merged"] = [craft\helpers\Template::attribute($this->env, $this->source,         // line 6
(isset($context["key"]) || array_key_exists("key", $context) ? $context["key"] : (craft\helpers\Template::fallbackExists("key") ? craft\helpers\Template::fallback("key") : (function () { throw new RuntimeError('Variable "key" does not exist.', 6, $this->source); })())), "nested"), "baz" => "bat"];

Definitely looks to me like it's related to one of Craft's node visitors, similar to #14783

@brandonkelly brandonkelly reopened this Apr 22, 2024
@brandonkelly brandonkelly self-assigned this Apr 22, 2024
@brandonkelly
Copy link
Member

Doh, should have thought to test on Twig Fiddle. I tested locally with a simple hash/array:

{% set hash = {
  one: 1,
  two: 2,
} %}

{% dd {
  ...hash,
  three: ['four'],
} %}
#}

And that worked fine, so I figured it was a Twig bug with arrays that come from object methods.

Fixed for the next release!

@MoritzLost
Copy link
Contributor Author

@brandonkelly Tricky Bug 🐛 Thanks for the fix!

@brandonkelly
Copy link
Member

Craft 4.8.10 and 5.0.5 are out with that fix. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants