Putting the HYPER in hypermedia

htmx 4 in Drupal

Brendan Tuckerman

November 19, 2026

Let’s get H Y P E D

  • I wonder if…

  • I should look into…

Let’s get H Y P E D

  • Three reasons to get hyped

  • A re(?)introduction to htmx

  • htmx 4 in Drupal 12

Thanks up front

Who is this presentation for?

A group of people wearing hypercolour tshirts
  • Hopefully everyone.

  • Some understanding of HTTP requests & responses helps.

  • Show, don’t tell.

3 Reasons to get hyped

1. All the C O O L kids love htmx & hypermedia

a boy in fluro 90s clothing

Rule of least power (and simplicity)

A winged skateboard

2. JS and JSON API fatigue

[Complexity, JSON API and JavaScript churn] has led to a phenomenon known as “JavaScript Fatigue.” This refers to a general sense of exhaustion with all the hoops that are necessary to jump through to get anything done in modern-day web applications.

Carson Gross, “Hypermedia Systems”

3. htmx is (increasingly) part of Drupal

Diagram of packages in Drupal being replaced with htmx
Source: Drupal.org

Re(?) introduction to htmx

How familiar are you?

The exterior of the Tuggernanong hyperdome shopping center
  1. Not very familiar

  2. Some familiarity

  3. Very familiar

htmx generalises hypermedia controls

Reebok pumps

htmx responds to the following

  1. All elements should be able to issue an HTTP request, not just <a> and <form>.
  2. Any event should be able to trigger that request.
  3. Any HTTP method should be available, not just POST or GET.
  4. A request should allow for responses to replace parts of a document, not just the whole thing.

Basic htmx example

<button
hx-post="/clicked"
hx-trigger="click"
hx-target="#output"
hx-swap="innerHTML"
>
Click Me
</button>
<p id="output">Update will appear here...</p>

Drupal Twig

{{ attach_library('core/drupal.htmx')}}
<button
data-hx-get="{{ path('htmxdemo.basic_response') }}"
data-hx-trigger="click"
data-hx-target="#htmx-basic-target"
data-hx-swap="innerHTML"
data-hx-select="#htmx-basic-select"
> Click me
</button>
<p><span id="htmx-basic-target">Update will appear here...</span></p>

A few more bells and whistles

{{ attach_library('core/drupal.htmx')}}
<p>Hover over me to get a
<span
data-hx-get="{{ path('htmxdemo.random_response') }}"
data-hx-trigger="mouseover delay:100ms"
data-hx-target="#htmx-random-target"
data-hx-swap="innerHTML swap:1s"
data-hx-sync="abort"
data-hx-indicator=".htmxdemo-spinner"
> R A N D O M </span> response.</p>
<img id="spinner"
class="htmxdemo-spinner"
src="/some/spinner/image"
alt="A spinner image">
<p><span id="htmx-random-target">Update here...</span></p>

Via a controller

// Inside a controller
foreach ($range as $item) {
$id = 'page_' . $item;
$items[$id] = [
'#type' => 'html_tag',
'#tag' => 'a',
'#value' => $this->t('Page @number', ['@number' => $item]),
'#attributes' => [
'name' => $id,
'href' => '#',
],
];
(new Htmx())
->get()
->swap('outerHTML')
->target('#detail')
->trigger('click')
->applyTo($items[$id]);
}

AJAX callback -> htmx in Drupal

core/modules/config/src/Form/ConfigSingleExportForm.php
// Before 11.3; ajax callback
$form['config_type'] = [
'#type' => 'select',
// ...
'#ajax' => [
'callback' => '::updateConfigurationType',
'wrapper' => 'edit-config-name-wrapper',
],
];
// Callback updateConfigurationType handles the logic.
core/modules/config/src/Form/ConfigSingleExportForm.php
// After 11.3; htmx
$form_url = Url::fromRoute('config.export_single');
(new Htmx())
->post($form_url)
->onlyMainContent()
->select('[data-drupal-wrapper-selector="edit-config-name"]')
->target('[data-drupal-wrapper-selector="edit-config-name"]')
->swap('outerHTML')
->trigger('change') // Not needed. Just for this example
->applyTo($form['config_type']);

htmx 4 in Drupal 12

Some of what’s new

  • fetch() replaces XMLHttpRequest
  • Inheritance is no longer implied
  • <hx-partial>
  • Events syntax has changed.
  • A number attributes including hx-query

TL;DR

  • Stuff has changed from htmx 2 -> 4
  • Check for deprecations
  • Check the docs

Explicit Inheritance

This used to work:

<div data-hx-target="#results">
{% for item in items %}
<button data-hx-get="{{ path('my_module.item', {'id': item.id}) }}">
{{ item.label }}
</button>
{% endfor %}
</div>
<div id="results"></div>

But now we need explicit inheritance:

<div data-hx-target:inherited="#results">
{% for item in items %}
<button data-hx-get="{{ path('my_module.item', {'id': item.id}) }}">
{{ item.label }}
</button>
{% endfor %}
</div>

Inheritance in Drupal

(new Htmx())
->target('#someId', Htmx::INHERITED)
->applyTo($form['someWrapper']);

<hx-partial>

{# partial.twig #}
<button data-hx-get="{{ path('htmxdemo.partials_roll') }}" >Roll</button>
<h3>Latest roll</h3>
<p><output id="roll-latest">-</output></p>
<h3>History</h3>
<ol id="roll-history"></ol>

{# partial_response.twig #}
<hx-partial hx-target="#roll-latest" hx-swap="innerHTML">{{ roll }}</hx-partial>
<hx-partial hx-target="#roll-history" hx-swap="beforeend"><li>{{ roll }}</li></hx-partial>

Events: hx-on

// Save when user presses ctrl anywhere
// We could also ask it to run our own JS function--not the htmx JS API example here.
(new Htmx())
->on([
"keydown[ctrlKey] from:document" =>
"htmx.trigger(#someID, 'dashboard-save')",
])
->applyTo($build);

Events: HX-Trigger header

//HX-Trigger
(new Htmx())
->triggerHeader(['some-event' => ['target' => 'body']])
->applyTo($form);

Dashboard with Alpine

{# Alpine has a magic helper for custom events. #}
{# Here we fire off 'dashboard-sorted' event using $dispatch #}
<div{{ grid_attributes }}
x-sort.ghost="$dispatch('dashboard-sorted')"
>
{{ cards }}
</div>

// Now we can use that event as our trigger. The POST here 'saves' the card layout
(new Htmx())
->post(Url::fromRoute('htmxdemo.dashboard.sort'))
->trigger('dashboard-sorted')
->swap('none')
->applyTo($element);

Bonus

HTML spec and HTTP spec

RFC 10008: The HTTP QUERY Method

Want a normal / htmx only response?

HtmxRequestInfoTrait::isHtmxRequest()

.routing.yml
htmxdemo.dashboard.card_add:
path: "/htmxdemo/dashboard/card"
defaults:
_controller: '\Drupal\htmxdemo\Controller\HtmxdemoDashboardController::addCard'
methods: [POST]
requirements:
_permission: "access content"
_csrf_request_header_token: "TRUE"
options:
_htmx_route: TRUE

HX-Request in the request headers.

->onlyMainContent()

A search with QUERY

// For this demo, we can toggle between GET and QUERY
$searchUrl = Url::fromRoute('htmxdemo.search_route');
$search = new Htmx();
if ($method === 'query') {
$search->query($searchUrl);
}
else {
$search->get($searchUrl);
}
$search
->onlyMainContent()
->trigger('input changed delay:300ms')
->target('#search-results')
->swap('innerHTML')
->pushUrl(TRUE)
->applyTo($form['search_wrapper']['search']);

hx-boost and view transitions

No JavaScript? No problem.

Wrapping up…

Stuff that is really cool but I didn’t get a chance to cover

  • hx-boost
  • View Transition API
  • Extensions
  • Idiomorph
  • Server side events
  • Websockets
  • …

Thanks!

Stay Rad
  • I wonder if…

  • I should look into…

Resources

Hypermedia Systems