-
Notifications
You must be signed in to change notification settings - Fork 639
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
Comments
That’s something you’ll need to report on the Twig repo. |
@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:
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: 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 |
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! |
@brandonkelly Tricky Bug 🐛 Thanks for the fix! |
Craft 4.8.10 and 5.0.5 are out with that fix. Thanks for reporting! |
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:
This should result in a flat array with keys
foo
,alice
andthree
, but I'm getting a nested array instead: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
The text was updated successfully, but these errors were encountered: