Problem/Motivation
A component details page fails if there are no slots defined on the metadata.
The error is as follows:
An exception has been thrown during the rendering of a template ("[slots] Array value found, but an object is required")
Example of component metadata used:
'$schema': 'https://git.drupalcode.org/project/drupal/-/raw/10.1.x/core/modules/sdc/src/metadata.schema.json'
name: "Button"
status: "stable"
description: "Use Bootstrapโs custom button styles for actions in forms, dialogs, and more with support for multiple sizes, states, and more."
props:
type: object
properties:
type:
type: "string"
title: "Type"
enum:
- default
- outline
style:
type: "string"
title: "Style"
enum:
- primary
- secondary
- success
- danger
- warning
- info
- light
- dark
- link
size:
type: "string"
title: "Size"
enum:
- large
- small
disabled:
type: "boolean"
title: "Disabled"
toggle:
type: "boolean"
title: "Toggle"
Steps to reproduce
- Create a component without slots.
- Try to access its details page using cl_devel. The error ocurs
- Adding a slot to the metadata solves the issue.
Proposed resolution
The problem happens when $metadata->slots
is empty and is evaluated as an empty array, whereas component-details.component.yml
requires slots to be an object.
return [
'#type' => 'component',
'#component' => 'cl_devel:component-details',
'#props' => [
'attributes' => new Attribute(),
'machineName' => $metadata->machineName,
'id' => $component_id,
'name' => $metadata->name,
'description' => $metadata->description,
'status' => $metadata->status,
'thumbnailHref' => $href,
'path' => $metadata->path,
'props' => $metadata->schema ?? new \stdClass(),
'slots' => !empty($metadata->slots) ? $metadata->slots : new stdClass,
],
'#slots' => [
'documentation' => [
'#markup' => Xss::filterAdmin($docs->getContent()),
],
],
];
The quickest solution (hotfix) is to pass an empty object when slots are empty:
'slots' => !empty($metadata->slots) ? $metadata->slots : new stdClass,
A better solution could be to populate the slots only after performing a check.
Remaining tasks
Implement solution
User interface changes
None
API changes
None
Data model changes
None