Problem/Motivation
We are planning to rewrite UI Patterns upon SDC. To achieve this, we are proposing a few changes and additions:
- #3390980: Make SDC extensible
- #3352063: Allow schema references in Single Directory Component prop schemas
- #3390712: Add component variants to SDC
- #3390717: Allow declaration of template path in SDC
- SDC ComponentElement: Transform slots scalar values to #plain_text. The current issue
- #3391978: SDC: Add a method to retrieve only components not replaced by others
Today, #slots property of ComponentElement accepts only render arrays.
If something else is send, there is a fatal error:
Unable to render component "%s". A render array is expected for the slot "%s" when using the render element with the "#slots" property
We believe such a strong behaviour and expected verbosity don't fit with developers expectations and may hurt SDC adoption.
Steps to reproduce
When using component render element, instead of doing this:
[
'#type' => 'component',
'#component' => 'ui_patterns_test:close_button',
'#slots' => [
"foo" => [
"#plain_text" => "Bar",
]
],
'#props' => []
];Do that:
[
'#type' => 'component',
'#component' => 'ui_patterns_test:close_button',
'#slots' => [
'foo' => "Bar",
],
'#props' => []
];Proposed resolution
More or less something like that:
@@ -105,13 +105,10 @@ class ComponentElement extends RenderElement {
$template .= sprintf('{%% embed \'%s\' %%}', $id);
$template .= PHP_EOL;
foreach ($slots as $slot_name => $slot_value) {
- if (!Utilities::isRenderArray($slot_value)) {
- $message = sprintf(
- 'Unable to render component "%s". A render array is expected for the slot "%s" when using the render element with the "#slots" property',
- $id,
- $slot_name
- );
- throw new InvalidComponentDataException($message);
+ if (!Utilities::isRenderArray($slot_value) && \is_scalar($slot_value)) {
+ $slot_value = [
+ "#plain_text" => (string) $slot_value,
+ ];
}
$context[$slot_name] = array_reduce(
$slots_alter_callbacks,
With update of related tests.
Remaining tasks
If there is a chance for this change to be accepted, we (UI Patterns team) can propose a merge request soon.
We have one month before the release of Drupal 10.2.0-alpha1.
API changes
It seems safe. Nothing breaking.
Issue fork drupal-3391702
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
g4mbiniComment #3
idiaz.roncero+1
Comment #4
e0ipsoI think this is in line with the Robustness Principle, and I am in favor.
What happens if a non-string scalar is passed? Do we trigger the error then, do we cast it to a string, or do we find an appropriate render array to use for each scenario?
Comment #5
pdureau commentedLet's raise the
InvalidComponentDataExceptionyou are currently raising.Comment #9
pdureau commentedComment #11
pdureau commentedBefore doing any modifications, I got some failures from module's tests:
Maybe I did something wrong while setting up my phpunit test environement.
Those failures will stay after my modification, so I hope I will not miss a test case.
Comment #12
pdureau commentedHi Mateu,
Here is the merge request: https://git.drupalcode.org/project/drupal/-/merge_requests/5044
I have also updated the
checkInvalidSlottest:I wanted to use a PHP array which is not a render array instead of stdClass() . However, it seems Utilities::isRenderArray() is returning TRUE for any array, not only render array. Am I right? If yes, do we need to fix this too?
Comment #13
smustgrave commentedCC failure in MR
Have not tested.
Comment #14
pdureau commentedI pushed a second commit: https://git.drupalcode.org/project/drupal/-/merge_requests/5044/diffs?co...
I hope it is OK now.
Comment #15
smustgrave commentedRebased to run test-only feature
Which is good.
The change looks good and matches the proposed solution. Only question I have is even though SDC is experimental will it need a CR for the new way to pass things?
Comment #16
e0ipsoAgreed on the RTBC. I think this improves DX a bit.
Thanks for this!
Comment #21
lauriiiCreated a CR and committed 76fac09 and pushed to 11.x. Cherry-picked all the way to 10.1.x because SDC is experimental. Thanks!