Direkt zum Inhalt

Devbits

Kleine Code-Schnipsel aus unserem Alltag mit Drupal, PHP und DevOps: Problem, Lösung, Code.

23.10.2026 | Lothar Ferreira Neumann

How to fill empty form fields via a custom Drupal AJAX command

In many editor or form workflows fields need to be prefilled (for example based on a selected entity or computed data) without overwriting input the user may have already entered.

<?php

declare(strict_types=1);

namespace Drupal\my_module\Ajax;

use Drupal\Core\Ajax\CommandInterface;

/**
 * Ajax command: fill empty form elements with a predefined value.
 *
 * This command targets one or more form elements via a CSS selector and sets
 * their value only if the current value is empty (after trimming).
 *
 * The command also triggers `input` and `change` events so Drupal behaviors and
 * other listeners react as if the user entered the value manually.
 */
final class SetValueIfEmptyCommand implements CommandInterface {

  /**
   * The Ajax command name used by Drupal.AjaxCommands.
   */
  public const COMMAND = 'setValueIfEmpty';

  /**
   * Default selector used when none is provided.
   */
  public const DEFAULT_SELECTOR = '[data-my-fill-target]';

  /**
   * Creates a command instance.
   *
   * @param string $fillValue
   *   The value to set when the target element is empty.
   * @param string|null $targetSelector
   *   CSS selector for the target elements. If omitted, DEFAULT_SELECTOR is used.
   */
  public function __construct(
    protected string $fillValue,
    protected ?string $targetSelector = null,
  ) {}

  /**
   * {@inheritdoc}
   */
  public function render(): array {
    return [
      'command' => self::COMMAND,
      'selector' => $this->targetSelector ?? self::DEFAULT_SELECTOR,
      'value' => $this->fillValue,
    ];
  }

}
/**
 * @file Field empty check.
 */
(function (Drupal) {
  'use strict';

  Drupal.AjaxCommands.prototype.setValueIfEmpty = function (ajax, response) {
    const targetSelector = response && response.selector ? response.selector : '';
    const fillValue = response && typeof response.value === 'string' ? response.value : '';

    if (!targetSelector) {
      return;
    }

    const elements = document.querySelectorAll(targetSelector);
    for (const element of elements) {
      let currentValue = '';

      if (typeof element.value === 'string') {
        currentValue = element.value.trim();
      }
      else if (element.isContentEditable) {
        currentValue = (element.textContent || '').trim();
      }

      if (currentValue !== '') {
        continue;
      }

      if (typeof element.value === 'string') {
        element.value = fillValue;
      }
      else if (element.isContentEditable) {
        element.textContent = fillValue;
      }
      else {
        continue;
      }

      element.dispatchEvent(new Event('input', { bubbles: true }));
      element.dispatchEvent(new Event('change', { bubbles: true }));
    }
  };

})(Drupal);
set_value_if_empty:
  js:
    js/set_value_if_empty.js: {}
  dependencies:
    - core/drupal
    - core/drupal.ajax
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\my_module\Ajax\SetValueIfEmptyCommand;

$response = new AjaxResponse();

// Example: fill a media name field if it's empty.
$fill_value = 'foo-bar';

// Selector for a specific element in this use-case.
$target_selector = '[data-drupal-selector$="my_field"]';

$response->addAttachments([
  'library' => [
    'my_module/set_value_if_empty',
  ],
]);

$response->addCommand(new SetValueIfEmptyCommand($fill_value, $target_selector));

return $response;

Issue

In many editor or form workflows fields need to be prefilled (for example based on a selected entity or computed data) without overwriting input the user may have already entered.

Built-in AJAX setters such as InvokeCommand('val') always assign values unconditionally. When an AJAX update happens after the user started typing, this can unintentionally replace user-provided content.

Prerequisites

This approach only works when executed within an AJAX callback returning an AjaxResponse.

Solution

Use a lightweight custom AJAX command that runs in the browser and checks the current value of the target element(s).
The command sets the provided value only if the field is empty (after trimming).

The server supplies the selector and value while the client performs the final emptiness check and applies the update conditionally, preventing accidental overwrites of user input.

13.04.2026 | Pascal Crott

Restore AJAX functionality when using Slick Slider and Layout Builder

When you are using Layout Builder, AJAX for a Slick Slider may break.

Uses new Drupal "once" Library without using JQuery (see: https://www.drupal.org/node/3158256).

          slick.on('breakpoint', function (e, _) {
            if (settings.isLayoutBuilder) {
              once.remove('ajax', '.use-ajax', _.$slideTrack[0]);
              Drupal.ajax.bindAjaxLinks(_.$slideTrack[0]);
            }
          });

Issue

When you are using Layout Builder, AJAX for a Slick Slider may break.

Prerequisites

Layout Builder with a Slick Slider and Drupal 9.X or newer are in use

Solution

Use the snippet provided to re-bind AJAX events.

26.02.2026 | Holger Weischenberg

Using CSS media queries and CSS variables in JavaScript

You want JavaScript to execute something based on a breakpoint in a CSS media query.

// Retrieve all computed styles from the root HTML element (:root)
const rootStyles = getComputedStyle(document.documentElement);

// Get the value of the CSS variable --Breakpoint.
// Use a fallback (e.g., 768px) if the variable is not defined.
const breakpoint = rootStyles.getPropertyValue('--Breakpoint').trim() || '768px';

// Create a MediaQueryList based on the breakpoint value
const mediaQuery = window.matchMedia(`(min-width: ${breakpoint})`);

// Function that runs when the viewport is at or above the breakpoint
function handleDesktopLayout() {
    console.log("Viewport is at or above the breakpoint.");
}

// Function that runs when the viewport drops below the breakpoint
function handleMobileLayout() {
    console.log("Viewport is below the breakpoint.");
}

// Early return for the initial state:
// If the viewport does NOT meet the breakpoint requirement, run mobile logic and exit
if (!mediaQuery.matches) {
    handleMobileLayout();
    // No desktop logic needed at this point
} else {
    handleDesktopLayout();
}

// Listen for changes in viewport size relative to the breakpoint
mediaQuery.addEventListener('change', (event) => {
    if (event.matches) {
        // The viewport just became equal to or larger than the breakpoint
        handleDesktopLayout();
    } else {
        // The viewport just became smaller than the breakpoint
        handleMobileLayout();
    }
});

Issue:

You want JavaScript to execute something based on a breakpoint in a CSS media query.

Solution:

Read the breakpoint value directly from the --Breakpoint CSS variable, create a matchMedia query from it, and use an event listener to run JavaScript whenever the viewport crosses that breakpoint.

29.01.2026 | Peter Gerken

Manipulate Form Data before Submit

Using the provided solution will enable sharing of a set filter and ensures that whoever opens the link will see all teasers corresponding to the exposed filters. The map will be fully zoomed out so no teasers are 'accidentally' filtered out.

// Modifies form data on submit.
form.addEventListener("formdata", (e) => {
  const formData = e.formData;
  geoLocationFieldNames.forEach((name) => {
    formData.delete(name);
  });
});

Issue:

  • A list of teasers should be filtered by the set "area of detail" (boundaries) on an embedded map and additionaly by exposed filters
  • A filtered version of the view should be shareable by link, ensuring no items are unwantedly filtered out

Solution:

  • use "formdata event" which is supported by recent versions of all major browsers
  • form data is being manipulated on submit
  • map viewport boundaries will be injected into the get request, but not included into exposed filters

Conclusion:

Using the provided solution will enable sharing of a set filter and ensures that whoever opens the link will see all teasers corresponding to the exposed filters. The map will be fully zoomed out so no teasers are 'accidentally' filtered out.

13.11.2025 | Peter Gerken

Open all HTML details elements via browser console

You are on a page which has a large amount of HTML details elements, for instance translations, and you need to find a certain bit of information by searching the page.

let details = document.getElementsByTagName('details');
for (i = 0; i < details.length; ++i) {
  details.item(i).open = true;
}

Issue:

You are on a page which has a large amount of HTML details elements, for instance translations, and you need to find a certain bit of information by searching the page.

Solution:

The provided snippet opens all details on a page. This can be copy-and-pasted to the browser console to easily access all the fields hidden behind the nested details.

18.06.2024 | Pascal Crott

Load an object from another object by value of a certain key

You need to load an object by a value in a nested data structure, e.g. by its ID.

/**
 * Loads an object from another object by value of a certain key.
 *
 * @usage 
 *   data = [
 *     'foo': {
 *         'id' => 5,
 *         'label' => 'Foo'
 *     },
 *     'bar': {
 *         'id' => 6,
 *         'label' => 'Bar'
 *     }
 *   ]
 *   foo = getObjectFromDataByKeyValue('id', 5, data)
 *
 * @param key
 *   The key we are comparing our value with.
 * @param value
 *   The value the key should have.
 * @param data
 *   The data you want to search in.
 * @returns {unknown}
 */
function getObjectFromDataByKeyValue(key, value, data) {
  return Object.values(data).find(o => o[key] === value)
}

Issue:

You need to load an object by a value in a nested data structure, e.g. by its ID.

Solution:

Utilize the code snippet provided.