DX object to manage htmx attributes

Created on 10 May 2025, 25 days ago

Problem/Motivation

HTMX is designed as an extension of HTML. It is therefore a declarative markup system that uses attributes.

<button hx-get="/contacts/1" hx-target="#contact-ui"> <1>
    Fetch Contact
</button>

Instead, we have declarative attributes much like the
href attribute on anchor tags and the action
attribute on form tags. The hx-get attribute tells htmx:
β€œWhen the user clicks this button, issue a GET request to
/contacts/1.” The hx-target attribute tells
htmx: β€œWhen the response returns, take the resulting HTML and place it
into the element with the id contact-ui.”

from Hypermedia Systems

HTMX is just as happy with data-hx-get and so we will maintain standard markup in our implementation.

HTMX uses over 30 such attributes.

Proposed resolution

Rather than expecting Drupal developers to learn all the names and data requirements of all those attributes, create a class with methods to abstract the details of these attributes. These methods would both document and collect the necessary data.

Our current HTML Attribute object does not implement an interface. Create a shared interface that can identify Attribute and HtmxAttribute objects and move shared methods to a trait or traits.

Alter \Drupal\Core\Template\AttributeHelper::mergeCollections and Attribute::merge() to operate on the interface.

Support the HTMX architecture that some attribute values are either JSON or a concatenation of a string to JSON. These attributes should use the XSS::filter() method rather than Html::escape to support the double quotes needed for JSON. They also need to implement the single quoted syntax for HTML attributes.

  • Create AttributeJson
  • Create AttributeHtmxString to encapsulate the filter change.

Remaining tasks

User interface changes

None

Introduced terminology

None

API changes

HtmlAttributeInterface abstracts features of \Drupal\Core\Template\Attribute.

Data model changes

Two new attribute value objects

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

render system

Created by

πŸ‡ΊπŸ‡ΈUnited States fathershawn New York

Live updates comments and jobs are added and updated live.
  • Needs release note

    The major change should have a special release note written to summarize the importance of the change. See Write a release note for an issue.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024