Allow attributes on array item properties

Created on 25 September 2023, 9 months ago
Updated 26 September 2023, 9 months ago

Problem/Motivation

This is a followup to ✨ Process attributes coming from Storybook Fixed that would require ✨ Allow class types in item properties in SDC Active .

It would be useful to be able to use attributes on the items of an array property.

Steps to reproduce

Example component:

$schema: https://git.drupalcode.org/project/drupal/-/raw/10.1.x/core/modules/sdc/src/metadata.schema.json
name: Main menu
description: Menu main
libraryOverrides:
  dependencies:
    - core/once
props:
  type: object
  properties:
    attributes:
      title: "Attributes"
      type: Drupal\Core\Template\Attribute
    foo:
      type: array
      items:
        type: object
        properties:
          attributes:
            title: "Attributes"
            type: Drupal\Core\Template\Attribute

And story:

stories:
  - name: Menu main
    args:
      attributes:
        class: [ 'menu--dgr' ]
      foo:
        - attributes:
            class: [ 'menu-item' ]
        - attributes:
            class: [ 'menu--main' ]

Proposed resolution

ServerEndpointController::generateRenderArray() should handle attributes on array item properties.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France prudloff Lille

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

Comments & Activities

  • Issue created by @prudloff
  • @prudloff opened merge request.
  • πŸ‡«πŸ‡·France prudloff Lille

    The MR adds support for the first level of array items, but it would probably be best to implement this recursively.

  • πŸ‡«πŸ‡·France prudloff Lille
  • e0ipso Can Picafort

    Thanks for the contribution! I took a quick look at this. My first question is, would this also be necessary for objects (and not just arrays).

    My second question is, this would not be an issue if each one of the array items was a component itself. Is there a use case not to turn it into one?

  • πŸ‡«πŸ‡·France prudloff Lille

    My first question is, would this also be necessary for objects (and not just arrays).

    I don't think we have a use case for this but I guess it would make sense to allow it on objects as well.

    My second question is, this would not be an issue if each one of the array items was a component itself. Is there a use case not to turn it into one?

    We discussed this internally and while we agree it is cleaner if each item is a component, some core templates like menu.html.twig render both the root attributes and each item attribute in the same template. And for this kind of template, we want to stick as closely as possible to what the core does but we still want to be able to write stories with example attributes.

Production build 0.69.0 2024