Issue #3391702 by pdureau, smustgrave, e0ipso: SDC ComponentElement: Transform slots scalar values to #plain_text instead of throwing an exception
(cherry picked from commit 76fac09c84
)
merge-requests/5220/head
parent
e65ed0b26c
commit
2709718902
|
@ -105,9 +105,14 @@ class ComponentElement extends RenderElement {
|
|||
$template .= sprintf('{%% embed \'%s\' %%}', $id);
|
||||
$template .= PHP_EOL;
|
||||
foreach ($slots as $slot_name => $slot_value) {
|
||||
if (\is_scalar($slot_value)) {
|
||||
$slot_value = [
|
||||
"#plain_text" => (string) $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',
|
||||
'Unable to render component "%s". A render array or a scalar is expected for the slot "%s" when using the render element with the "#slots" property',
|
||||
$id,
|
||||
$slot_name
|
||||
);
|
||||
|
|
|
@ -42,6 +42,7 @@ final class ComponentRenderTest extends ComponentKernelTestBase {
|
|||
$this->checkLibraryOverrides();
|
||||
$this->checkAttributeMerging();
|
||||
$this->checkRenderElementAlters();
|
||||
$this->checkSlots();
|
||||
$this->checkInvalidSlot();
|
||||
}
|
||||
|
||||
|
@ -284,7 +285,36 @@ final class ComponentRenderTest extends ComponentKernelTestBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Ensure that the slots receive a render array when using the render element.
|
||||
* Ensure that the slots allow a render array or a scalar when using the render element.
|
||||
*/
|
||||
public function checkSlots(): void {
|
||||
$slots = [
|
||||
'This is the contents of the banner body.',
|
||||
[
|
||||
'#plain_text' => 'This is the contents of the banner body.',
|
||||
],
|
||||
];
|
||||
foreach ($slots as $slot) {
|
||||
$build = [
|
||||
'#type' => 'component',
|
||||
'#component' => 'sdc_test:my-banner',
|
||||
'#props' => [
|
||||
'heading' => $this->t('I am a banner'),
|
||||
'ctaText' => $this->t('Click me'),
|
||||
'ctaHref' => 'https://www.example.org',
|
||||
'ctaTarget' => '',
|
||||
],
|
||||
'#slots' => [
|
||||
'banner_body' => $slot,
|
||||
],
|
||||
];
|
||||
$crawler = $this->renderComponentRenderArray($build);
|
||||
$this->assertNotEmpty($crawler->filter('#sdc-wrapper [data-component-id="sdc_test:my-banner"] .component--my-banner--body:contains("This is the contents of the banner body.")'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the slots throw an error for invalid slots.
|
||||
*/
|
||||
public function checkInvalidSlot(): void {
|
||||
$build = [
|
||||
|
@ -297,11 +327,11 @@ final class ComponentRenderTest extends ComponentKernelTestBase {
|
|||
'ctaTarget' => '',
|
||||
],
|
||||
'#slots' => [
|
||||
'banner_body' => 'I am an invalid render array.',
|
||||
'banner_body' => new \stdClass(),
|
||||
],
|
||||
];
|
||||
$this->expectException(InvalidComponentDataException::class);
|
||||
$this->expectExceptionMessage('Unable to render component "sdc_test:my-banner". A render array is expected for the slot "banner_body" when using the render element with the "#slots" property');
|
||||
$this->expectExceptionMessage('Unable to render component "sdc_test:my-banner". A render array or a scalar is expected for the slot "banner_body" when using the render element with the "#slots" property');
|
||||
$this->renderComponentRenderArray($build);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue