AgentSkillsCN

htmx-development

当在 Drupal 11.3+ 中开发 HTMX 功能,或将 AJAX 迁移至 HTMX 时使用——涵盖 HTMX 类的使用、表单模式、迁移策略以及验证流程。可通过“HTMX”、“AJAX 转 HTMX”、“动态表单”、“依赖下拉菜单”等短语触发。

SKILL.md
--- frontmatter
name: htmx-development
description: Use when developing HTMX features in Drupal 11.3+ or migrating AJAX to HTMX. Covers Htmx class usage, form patterns, migration strategies, and validation. Triggers on "htmx", "ajax to htmx", "dynamic form", "dependent dropdown".
version: 1.2.0
model: sonnet

HTMX Development

Drupal 11.3+ HTMX implementation and AJAX migration guidance.

When to Use

  • Implementing dynamic content updates in Drupal
  • Building forms with dependent fields
  • Migrating existing AJAX to HTMX
  • Adding infinite scroll, load more, real-time validation
  • NOT for: Traditional AJAX maintenance (use ajax-reference.md)

Decision: HTMX vs AJAX

Choose HTMXChoose AJAX
New featuresExisting AJAX code
Declarative HTML preferredComplex command sequences
Returns HTML fragmentsDialog commands needed
Progressive enhancement neededContrib expects AJAX

Hybrid OK: Both systems coexist. Migrate incrementally.

Quick Start

1. Basic HTMX Element

php
use Drupal\Core\Htmx\Htmx;
use Drupal\Core\Url;

$build['button'] = [
  '#type' => 'button',
  '#value' => t('Load'),
];

(new Htmx())
  ->get(Url::fromRoute('my.content'))
  ->onlyMainContent()
  ->target('#result')
  ->swap('innerHTML')
  ->applyTo($build['button']);

2. Controller Returns Render Array

php
public function content() {
  return ['#markup' => '<p>Content loaded</p>'];
}

3. Route (Optional HTMX-Only)

yaml
my.content:
  path: '/my/content'
  options:
    _htmx_route: TRUE  # Always minimal response

Core Patterns

Pattern Selection

Use CasePatternKey Methods
Dependent dropdownForm partial updateselect(), target(), swap('outerHTML')
Load moreAppend contentswap('beforeend'), trigger('click')
Infinite scrollAuto-loadswap('beforeend'), trigger('revealed')
Real-time validationBlur checktrigger('focusout'), field update
Multi-step wizardURL-based stepspushUrl(), route parameters
Multiple updatesOOB swapswapOob('outerHTML:#selector')

Dependent Dropdown

php
public function buildForm(array $form, FormStateInterface $form_state) {
  $form['category'] = ['#type' => 'select', '#options' => $this->getCategories()];

  (new Htmx())
    ->post(Url::fromRoute('<current>'))
    ->onlyMainContent()
    ->select('#edit-subcategory--wrapper')
    ->target('#edit-subcategory--wrapper')
    ->swap('outerHTML')
    ->applyTo($form['category']);

  $form['subcategory'] = ['#type' => 'select', '#options' => []];

  // Handle trigger
  if ($this->getHtmxTriggerName() === 'category') {
    $form['subcategory']['#options'] = $this->getSubcategories(
      $form_state->getValue('category')
    );
  }

  return $form;
}

Reference: core/modules/config/src/Form/ConfigSingleExportForm.php

Multiple Element Updates

php
// Primary element updates via target
// Secondary element updates via OOB
(new Htmx())
  ->swapOob('outerHTML:[data-secondary]')
  ->applyTo($form['secondary'], '#wrapper_attributes');

URL History

php
(new Htmx())
  ->pushUrlHeader(Url::fromRoute('my.route', $params))
  ->applyTo($form);

Htmx Class Reference

Request Methods

  • get(Url) / post(Url) / put(Url) / patch(Url) / delete(Url)

Control Methods

  • target(selector) - Where to swap
  • select(selector) - What to extract from response
  • swap(strategy) - How to swap (outerHTML, innerHTML, beforeend, etc.)
  • swapOob(selector) - Out-of-band updates
  • trigger(event) - When to trigger
  • vals(array) - Additional values
  • onlyMainContent() - Minimal response

Response Headers

  • pushUrlHeader(Url) - Update browser URL
  • redirectHeader(Url) - Full redirect
  • triggerHeader(event) - Fire client event
  • reswapHeader(strategy) - Change swap
  • retargetHeader(selector) - Change target

See: references/quick-reference.md for complete tables

Detecting HTMX Requests

In forms (trait included automatically):

php
if ($this->isHtmxRequest()) {
  $trigger = $this->getHtmxTriggerName();
}

In controllers (add trait):

php
use Drupal\Core\Htmx\HtmxRequestInfoTrait;

class MyController extends ControllerBase {
  use HtmxRequestInfoTrait;
  protected function getRequest() { return \Drupal::request(); }
}

Migration from AJAX

Quick Conversion

AJAXHTMX
'#ajax' => ['callback' => '::cb'](new Htmx())->post()->applyTo()
'wrapper' => 'id'->target('#id')
return $form['element']Logic in buildForm()
new AjaxResponse()Return render array
ReplaceCommand->swap('outerHTML')
HtmlCommand->swap('innerHTML')
AppendCommand->swap('beforeend')
MessageCommandAuto-included

Migration Steps

  1. Identify #ajax properties
  2. Replace with Htmx class
  3. Move callback logic to buildForm()
  4. Use getHtmxTriggerName() for conditional logic
  5. Replace AjaxResponse with render arrays
  6. Test progressive enhancement

See: references/migration-patterns.md for detailed examples

Validation Checklist

When reviewing HTMX implementations:

  • Htmx class used (not raw attributes)
  • onlyMainContent() for minimal response
  • Proper swap strategy selected
  • OOB used for multiple updates
  • Trigger element detection works
  • Works without JavaScript (progressive)
  • Accessibility: aria-live for dynamic regions
  • URL updates for bookmarkable states

Common Issues

ProblemSolution
Content not swappingCheck target() selector exists
Wrong content extractedCheck select() selector
JS not runningVerify htmx:drupal:load fires
Form not submittingCheck post() and URL
Multiple swaps failAdd swapOob('true') to elements
History brokenUse pushUrlHeader()

References

Bundled (HTMX-Specific)

  • references/quick-reference.md - Command equivalents, method tables
  • references/htmx-implementation.md - Full Htmx class API, detection, JS
  • references/migration-patterns.md - 7 patterns with before/after code
  • references/ajax-reference.md - AJAX commands for understanding existing code

Online Dev-Guides (Drupal Domain)

For Drupal domain context when analyzing, recommending, or validating HTMX patterns, WebFetch from https://camoa.github.io/dev-guides/.

TopicURLUse when
AJAX form architecturedrupal/forms/ajax-architecture/Understanding existing AJAX before migration
AJAX securitydrupal/forms/ajax-security/Security review of AJAX patterns
JS AJAX integrationdrupal/js-development/ajax-integration/JS-level AJAX patterns to migrate
Drupal.behaviorsdrupal/js-development/drupal-behaviors-pattern/HTMX JS event integration context
Multi-step formsdrupal/forms/multi-step-forms/Wizard pattern migration context
Form #statesdrupal/forms/form-states-system/When #states is better than HTMX
Form alter systemdrupal/forms/form-alter-system/Altering forms with HTMX elements
Routingdrupal/routing/HTMX route configuration context
Render APIdrupal/render-api/Render arrays for HTMX responses
Render array patternsdrupal/render-api/render-array-patterns/Progressive enhancement patterns
Security best practicesdrupal/forms/security-best-practices/Form security for HTMX endpoints
JS testingdrupal/js-development/testing-javascript/Testing HTMX JS interactions

How to use: Prefix URLs with https://camoa.github.io/dev-guides/ and WebFetch for Drupal-specific patterns when the bundled HTMX references don't cover the underlying Drupal concept.

Key Files in Drupal Core

  • core/lib/Drupal/Core/Htmx/Htmx.php - Main API
  • core/lib/Drupal/Core/Htmx/HtmxRequestInfoTrait.php - Request detection
  • core/lib/Drupal/Core/Render/MainContent/HtmxRenderer.php - Response renderer
  • core/modules/config/src/Form/ConfigSingleExportForm.php - Production example
  • core/modules/system/tests/modules/test_htmx/ - Test examples