htmx 4 in Drupal
Brendan Tuckerman
November 19, 2026
I wonder if…
I should look into…
Three reasons to get hyped
A re(?)introduction to htmx
htmx 4 in Drupal 12
Shawn Duncan (Father Shawn) & htmx Drupal Slack

Hopefully everyone.
Some understanding of HTTP requests & responses helps.
Show, don’t tell.
[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.

Core in 11.3
htmx 4 in Drupal 12
Not very familiar
Some familiarity
Very familiar
<a> and <form>.<button hx-post="/clicked" hx-trigger="click" hx-target="#output" hx-swap="innerHTML"> Click Me</button><p id="output">Update will appear here...</p>{{ 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>{{ 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> // 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]); }// Before 11.3; ajax callback
$form['config_type'] = [ '#type' => 'select', // ... '#ajax' => [ 'callback' => '::updateConfigurationType', 'wrapper' => 'edit-config-name-wrapper', ],];
// Callback updateConfigurationType handles the logic.// 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']);fetch() replaces XMLHttpRequest<hx-partial>hx-queryThis 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>(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>// 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);//HX-Trigger
(new Htmx()) ->triggerHeader(['some-event' => ['target' => 'body']]) ->applyTo($form);{# 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);RFC 10008: The HTTP QUERY Method
HtmxRequestInfoTrait::isHtmxRequest()
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: TRUEHX-Request in the request headers.
->onlyMainContent()
// 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']);No JavaScript? No problem.
hx-boost
I wonder if…
I should look into…
Hypermedia Systems