Component details page fails if slots are not defined

Created on 9 June 2023, over 1 year ago
Updated 29 June 2023, over 1 year ago

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

  1. Create a component without slots.
  2. Try to access its details page using cl_devel. The error ocurs
  3. 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

๐Ÿ› Bug report
Status

Postponed: needs info

Version

2.0

Component

Code

Created by

๐Ÿ‡ช๐Ÿ‡ธSpain idiaz.roncero Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024