Component: Standardize on Attributes

Created on 12 December 2024, 7 days ago

Problem/Motivation

Currently we are inconsistent in the format we leverage classes and attributes. Our goal is to standardize on how we pass attributes into components.

Proposed resolution

At the top of components we would introduce:
{% set attributes = create_attribute(attributes) %}

Drupal Core describes attributes in the componet.yml files as this:

props:
  type: object
  properties:
    attributes:
      type: Drupal\Core\Template\Attribute
      title: Attributes
      description: Wrapper attributes.

Considering SDC Display do we need to to add a class variable that can be leveraged by UI users to attach custom classes?

User interface changes

We will need to test this change with examples leveraging SDC Display to ensure there are not breaking changes.

Component Examples

{{ @include('prototype:card', { 
  attributes: [ 
    id: 'my-super-neat-id',
    'aria-label': label
  ]}, with_context = false) }}
🌱 Plan
Status

Active

Version

5.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jldust

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

Comments & Activities

  • Issue created by @jldust
  • πŸ‡ΊπŸ‡ΈUnited States pixelwhip

    Should (and can) the type property be more flexible to allow passing in simple arrays of attributes? The template will support it but I'm not sure how that affects validation.

    Something like...

    props:
      type: object
      properties:
        attributes:
          type: ['Drupal\Core\Template\Attribute', 'object', 'null']
          title: Attributes
          description: Wrapper attributes.
    
  • πŸ‡ΊπŸ‡ΈUnited States sethhill

    I encountered issues with Drupal\Core\Template\Attribute and SDC Display, in particular. When including an Attribute, the component display form would not render. See πŸ› Drupal attribute properties break form generator Active for details.

    Even though the issue is closed as a duplicate there, the actual issue fix is on the schema-forms-php package and seems different. There wasn't a PR available there the last time I was setting up components, so this may resolve the issue I was experiencing.

Production build 0.71.5 2024