function HtmxRequestInfoTrait::getHtmxTarget

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Htmx/HtmxRequestInfoTrait.php \Drupal\Core\Htmx\HtmxRequestInfoTrait::getHtmxTarget()

Retrieves the target identifier from an HTMX request header.

HTMX 4 uses encodeURI to safely encode multilingual attribute identifiers for HX-Target. We follow that convention but use the name attribute or the data-drupal-selector value when available rather than id.

Values will be a CSS selector constructed from the first available property:

  • button[name="first_item"]
  • button[data-drupal-selector="first_item"]
  • button#id-value
  • button

Return value

string The value of the 'HX-Target' header, or an empty string if not set.

See also

core/assets/vendor/htmx/htmx.js:Htmx.#createRequestContext

core/misc/htmx/htmx-assets.js:htmx_config_request()

2 calls to HtmxRequestInfoTrait::getHtmxTarget()
FormBuilder::prepareForm in core/lib/Drupal/Core/Form/FormBuilder.php
Prepares a structured form array.
HtmxRequestInfoTest::testGetHtmxTarget in core/tests/Drupal/Tests/Core/Htmx/HtmxRequestInfoTest.php
Tests the getHtmxTarget method.

File

core/lib/Drupal/Core/Htmx/HtmxRequestInfoTrait.php, line 82

Class

HtmxRequestInfoTrait
Provides methods for getting information about the HTMX request.

Namespace

Drupal\Core\Htmx

Code

protected function getHtmxTarget() : string {
  return rawurldecode($this->getRequest()->headers
    ->get('HX-Target', ''));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.