Problem/Motivation
Following #3515506: Process #attributes render property for SDCs, testing 11.3-dev with a few SDC themes and projects, I have met TypeError: Drupal\Core\Template\Attribute::merge(): Argument #1 ($collection) must be of type Drupal\Core\Template\Attribute, array given when there is already a SDC attribute prop with an array value.
Steps to reproduce
Any component renderable with an array as attributes prop:
[
'#type' => 'component',
'#component' => 'foo:bar',
'#attributes' => [
'foo' => 'bar',
],
'#props' => [
'attributes' => [
'lorem' => 'ipsum',
]
]
]Proposed resolution
ComponentElement::mergeElementAttributesToPropAttributes() is already aware about this issue:
// If attributes value is an array, convert it to an Attribute object as
// \Drupal\Core\Template\Attribute::merge() expects an Attribute object.
But it is converting the other value:
$element_attributes = is_array($element['#attributes']) ? new Attribute($element['#attributes']) : $element['#attributes'];
So, let's keep this conversion and add the conversion of the value which must fit \Drupal\Core\Template\Attribute::merge() expectations.
API changes
We don't break anything. But we may need a new test.
Issue fork drupal-3550051
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
pdureau commentedComment #5
pdureau commentedMR opened, still iterating.
Comment #6
christian.wiedemann commentedLooks perfect.
Comment #7
christian.wiedemann commentedComment #8
christian.wiedemann commentedOnly one nitpic which is already wrong in the current MR
If Attributes is filled with "something" which is not an array and not attributes a type check would be good. (But this is also the case for attributes itself)
So a type check would increase the security:
Comment #9
christian.wiedemann commentedComment #10
pdureau commentedThanks Christian for your suggestion. It makes sense but I would prefer to keep the MR focused on the fatal error we are already observing in the wild.
Comment #11
astonvictor commentedIt's already fixed by #3515506: Process #attributes render property for SDCs
Here are changes that were committed - https://git.drupalcode.org/project/drupal/-/commit/103108a6668662adf5fe7...
Comment #13
astonvictor commentedComment #14
pdureau commentedHi Viktor,
As you can read in the issue description, this is not a duplicate of #3515506: Process #attributes render property for SDCs but a follow-up.
I am pretty confident about this because I am partially responsible of introducing this bug in #3515506: Process #attributes render property for SDCs where we check one of the two attributes data (
$element['#attributes']) without checking the other ($element['#props']['attributes']) 😳Comment #15
pdureau commentedTo also credit: @schillerm which has helped in the contribution room in Vienna.
Comment #16
schillerm commentedHi, yes I reproduced the error on 11.x and reviewed and tested !MR13394. Works as expected for me. +1 for RTBC
Had to learn more about working with core so that's good.
Comment #17
schillerm commentedComment #18
larowlanFix looks straight forward - let's get a test here - the steps to reproduce seem pretty simple
Thanks!
Comment #19
pdureau commentedOK, I will add or extend a test case in core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php
It will also be the opportunity to complete a missing part of the logic:
Comment #20
pdureau commentedHi @larowlan: Test added. Is it OK?
Comment #21
christian.wiedemann commentedLooks good now
Comment #22
christian.wiedemann commentedComment #23
pdureau commentedThanks Christian.
The last KO in pipleine is a functional test:
testNoLayoutSave with data set "revert":https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/layou...
There is no manipulation of SDC here, only User Session and Entity Storage. So I don't beleive it is related
Comment #24
alexpottCommitted and pushed 97116f5319d to 11.x and a64e7d785fb to 11.3.x. Thanks!
Backported to 11.3.x as a priority because this bug was introduced there.