Problem/Motivation

Document how to implement AlertCommand using htmx.

Remaining tasks

Document at https://www.drupal.org/docs/develop/drupal-apis/htmx/ajax-api-to-htmx/alertcommand

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

fathershawn created an issue. See original summary.

fathershawn’s picture

Issue summary: View changes
quietone’s picture

Version: 11.0.x-dev » 11.x-dev
fathershawn’s picture

Project: Drupal core » Documentation
Version: 11.x-dev »
Component: documentation » New documentation
markie’s picture

Status: Active » Needs review
Issue tags: +Chicago2026

This command generates a native alert box with the resulting text. Mostly in core this is used in tests. But here's a stab at the documentation.

## AJAX API

Used in tests such as web/core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;

  /**
   * Ajax form callback: Selects 'alert'.
   */
  public static function advancedCommandsAlertCallback($form, FormStateInterface $form_state): AjaxResponse {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand('Alert'));
    return $response;
  }

## HTMX

There is no native alert command in htmx, but you can use the on::afterrequest to trigger a command

use Drupal\Core\Htmx\Htmx;

(new Htmx())
  ->get($dialogUrl)
  ->on('::after-request', 'alert("Alert")');

### HTMX commands

markie’s picture

I updated my sample after reading more into the on().

fathershawn’s picture

Status: Needs review » Needs work

This command is not used in core but I see it is used in contrib.

We should consider a couple of improvements here:

  1. Recommend calling a JS function rather than the inline syntax
  2. If we implement an out of band swapping on system messages, then maybe we could just recommend setting a standard message?