Drupal htmx cheat-sheet
Table of Contents
This article at a glance…
Who is this for?
- Drupal developers
Why should I read this?
htmx was added to Drupal core in version 11.2, and has been fully integrated since 11.3. One significant implementation of this is the community initiative to replace the AJAX API and jQuery reliance.
htmx 4 was released in August 2026 and brings some new features and some breaking changes. htmx 4 is expected to ship with the Drupal 12 release.
This post explores the htmx code in Drupal in detail. It serves as a quick reference to what is available (currently) in Drupal core.
What is htmx?
If you need a recap on htmx in Drupal see my post where I go into detail.
Overview
htmx 4 is still a work in progress in Drupal, with a release due in Drupal 12. However, htmx 2.0 has been a part of Drupal core since Drupal 11.3.
htmx in Drupal core
htmx in core is lightweight, with only three PHP files:
Htmx.phpHtmxLocationResponseData.phpHtmxRequestInfoTrait.php
Htmx.php
Presents the htmx controls for developers to use with render arrays.
An htmx object can be created with $htmx = new Htmx();
Because it is the expected syntax in Drupal, attributes start with the data- prefix,
rather than the base hx-. So hx-get becomes data-hx-get.
htmx 4 removes the implied inheritance that existed in htmx 2. Previously, all child
elements would inherit the htmx properties of their ancestor. Now you need to mark
the parent as :inherited, so that the children receive the parent’s attributes unless they have
their own.
:append is an alternative modifier that adds a child’s value to the parent’s. If both
:append and :inherited are used, then the values will be combined.
Helper functions
The helper function keyWithModifiers has been added to create the data- attribute
and :inherited and :append, if selected. This occurs when a method is called
on an Htmx object.
There are several utility functions to set and get headers, and to set and remove attributes.
| Method | Params | Notes |
|---|---|---|
onlyMainContent |
Bool | This is a Drupalism that allows you to toggle between a full HTML response or just the main content. Note that TRUE is ‘only main content’, i.e. not the full HTML response. |
Header methods
These methods can be used to set htmx-specific headers:
| Method | htmx header | Notes |
|---|---|---|
locationHeader |
HX-Location |
Redirect without a full page load (AJAX) |
pushUrlHeader |
HX-Push-Url |
Pushes URL into the browser history |
replaceUrlHeader |
HX-Replace-Url |
Replaces current URL in browser history. Does not add a new history entry |
refreshHeader |
HX-Refresh |
If set to true, browser will refresh the (full) page |
reswapHeader |
HX-Reswap |
Override the swap style from the server. If the element has innerHTML this can specify outerHTML, for example |
retargetHeader |
HX-Retarget |
Override the target to a different element |
reselectHeader |
HX-Reselect |
Overrides the CSS selector |
triggerHeader |
HX-Trigger |
Set an event that will be triggered after swap with hx-trigger. Drupal encodes this as JSON if an array, a string if not. Use with on (hx-on) |
HTTP methods via hx-
These HTTP methods are provided via htmx attributes, and created with the prefix data-, using the following methods:
| Method | htmx attribute | Params | Notes |
|---|---|---|---|
get |
hx-get |
Both [URL (null sends to current page), modifiers] or none | GET request |
post |
hx-post |
Both [URL (null sends to current page), modifiers] or none | POST request |
put |
hx-put |
Both [URL (null sends to current page), modifiers] or none | PUT request |
patch |
hx-patch |
Both [URL (null sends to current page), modifiers] or none | PATCH request |
delete |
hx-delete |
Both [URL (null sends to current page), modifiers] or none | DELETE request |
query |
hx-query |
Both [URL (null sends to current page), modifiers] or none | QUERY request |
htmx attributes
These htmx attributes are added as data- attributes via the following methods:
| Method | htmx attribute | Params | Notes |
|---|---|---|---|
action |
hx-action |
Both [URL (null sends to current page), modifiers] or none | Specifies the URL that will receive the request. It mirrors the native action on forms. Can be used to process submission |
method |
hx-method |
HTTP method, modifiers (defaults to none) | The HTTP verb to use for the request. Allows all verbs (except QUERY) |
on |
hx-on |
Event name(s) [string or array], JS statement (defaults to ‘’), modifiers (defaults to none) | React to events |
pushUrl |
hx-push-url |
Bool or URL, modifiers | Push URL into history |
select |
hx-select |
CSS selector string, modifiers | What content to swap in from a response |
selectOob |
hx-select-oob |
CSS selectors (string, string[]), modifiers | Select content for an out-of-band swap |
status |
hx-status |
HTTP status code, strategy, modifiers | Allows swap behaviour dependent on response code; overrides the default hx-swap. Can be an HTTP status code or wildcard “50x” / “5xx” |
swap |
hx-swap |
Strategy, modifiers, bool ignoreTitle, int attributeModifiers | Specify how the response will be swapped |
swapOob |
hx-swap-oob |
true | swap strategy | strategy:CSS-selector |
Specifies that some content in a response should be swapped into a place in the DOM other than the target |
target |
hx-target |
Target, modifiers | Target an element outside of the one making the request |
trigger |
hx-trigger |
string | string[] definition, modifiers | When to trigger a request |
vals |
hx-vals |
Array in ‘name’ => ‘value’ form, modifiers | Values to pass as params in the request |
boost |
hx-boost |
Bool: should it be boosted? | Progressive enhancement for forms and a tags |
ignore |
hx-ignore |
None | Disable htmx for this node and its descendants |
disable |
hx-disable |
Descriptor, merge (parent values), modifiers | Uses the same descriptor syntax as hx-target; disables an element during a request |
encoding |
hx-encoding |
Method, modifiers | Change request encoding type |
historyElt |
hx-history-elt |
None | Marks the single element that should swap when using the back / forward button. Only this element is replaced |
headers |
hx-headers |
Array [name => value] headers, modifiers | Add headers to be sent with an htmx request |
include |
hx-include |
Descriptors, modifiers | What values to include. Same syntax as hx-target |
indicator |
hx-indicator |
Selector (CSS selector, see docs), modifiers | Used to create spinners etc. This actually just selects the element to place a .htmx-request class onto |
morphSkip |
hx-morph-skip |
Toggle [default = TRUE] | Marks a single element to be left untouched during a morph swap. htmx copies no attributes and morphs no children. Docs suggest using this for 3rd-party widgets and elements with animations or client-side state |
morphSkipChildren |
hx-morph-skip-children |
Toggle [default = TRUE] | Here the element will be morphed but the children will be left alone |
preserve |
hx-preserve |
Modifiers | Matching elements should be kept unchanged |
replaceUrl |
hx-replace-url |
URL object or bool, modifiers | Use a URL to replace the value in the location bar, or set to true or false |
config |
hx-config |
Config values (timeout, credentials, cache, redirect, referrer, integrity, validate), modifiers | Configure the behaviour of the request |
sync |
hx-sync |
CSS selector, modifiers | Sync AJAX requests between multiple elements |
validate |
hx-validate |
Bool value, modifiers | Validate self before request |
The following methods are extensions for Drupal:
| Method | Params | Notes |
|---|---|---|
applyTo |
Render array, attributeKey [defaults to #attributes] | Exports data from internal storage to a render array, i.e. adds htmx, headers, and attributes |
createFromRenderArray |
Render array, attributeKey [defaults to #attributes] | Create an htmx object from a render array |
HtmxLocationResponseData.php
Provides options for the HX-Location header. While this can take just a URL, there are 8 other settings that can be added,
such as overrides for swap and target. Also takes care of caching.
HtmxRequestInfoTrait.php
Methods for getting information about the htmx request.
Note it is a trait.
| Method | Notes |
|---|---|
getRequest |
Get the request object |
isHtmxRequest |
Returns bool. Use this to choose between, say, rendering a full template and rendering a fragment |
isHtmxBoosted |
Returns bool. Is the request boosted by htmx |
getHtmxCurrentUrl |
Returns the value of the HX-Current-URL header |
isHtmxHistoryRestoration |
Returns bool. Determines if the request is for history restoration, sent after a miss in the local history cache |
getHtmxTarget |
htmx 4 related: returns the value of the HX-Target header, which is encoded by htmx to safely encode multilingual attributes. Result will be a CSS selector |
getHtmxSource |
As above, but returns the value of the HX-Source header |
getHtmxTriggerName |
Gets the trigger from inside the HX-Source header |
getHtmxRequestType |
Value of the HX-Request-Type header ("full" | "partial") |