Add a methods to get info about which elements are involved in an HTMX Request

Created on 2 September 2025, 3 days ago

Problem/Motivation

Adding interactivity with HTMX will sometimes involve displays that depend on which element triggered the request. HTMX intends to return such data in a request header HX-Trigger and HX-Trigger-Name.
HTMX also provides information on the target of new markup in the HX-Targetheader.

We are assigning the value of HX-Trigger-Name to the _triggering_element_name input value in πŸ“Œ Support dynamic forms using HTMX Active .

Challenges

  1. HTMX is designed to work with any element and the name attribute is added only to form elements.
  2. HTMX provides the HX-Trigger and HX-Target headers. HX-Trigger and HX-Target both use the ID attribute but these can be programmatically altered in Drupal. In the HTMX contrib module we adjusted these header values our javascript to use the data-drupal-selector value.
  3. Developers will want to access this information both in both form building and display building.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

ajax system

Created by

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

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

Comments & Activities

  • Issue created by @fathershawn
  • πŸ‡ΊπŸ‡ΈUnited States fathershawn New York
  • πŸ‡ΊπŸ‡ΈUnited States fathershawn New York
  • πŸ‡ΊπŸ‡ΈUnited States richgerdes New Jersey, USA

    @fathershawn,

    Per our conversation at DrupalNYC Contribution day, i think it would be helpful to have a trait to parse the request headers. Something like...

    
    trait HTMXHeaders {
      public function getHtmxTrigger(): ?string;
      public function getHtmxTriggerName(): ?string;
    }
    
    

    These functions can look for the request ($this->request ?: \Drupal->getRequest()) and then get the headers.

    We also talked about the helpfulness of a getHtmxTriggerElement($build): ?array function which could locate the render element and to allow access to properties or data, but decided this wasn't critical at this time.

Production build 0.71.5 2024