Unable to put unicode characters in #attributes

Created on 6 January 2017, over 8 years ago
Updated 12 August 2025, 17 days ago

Problem/Motivation

#attributes gets converted into a \Drupal\Core\Template\Attribute object, with each attribute either needing to extend AttributeValueBase or being converted to one of Attribute[Array|Boolean|String]. These all call Html::escape on their values when converting to a string.

It is common to use data attributes (e.g. data-icon) to store icon characters coupled with CSS (e.g. content: attr(data-icon);) to output icons. It's common practice to use unicode html entities for this (e.g. ✔ for a tick). These then get escaped and render garbage.

Proposed resolution

Add a AttributeEscaped that you can use to indicate that you have already appropriately filtered and escaped the attribute value, who's __toString simply returns the value.

API changes

Addition of a new attribute for use in contrib.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

render system

Created by

πŸ‡¬πŸ‡§United Kingdom andrewbelcher

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This came up as a daily BSI target

    Can you provide real world scenario? Mention needing to store icon characters but my question is the why

Production build 0.71.5 2024