function Htmx::applyTo

Exports data from internal storage to a render array.

Parameters

mixed[] $element: The render array for the element.

string $attributeKey: Optional target key for attribute output: defaults to '#attributes'.

File

core/lib/Drupal/Core/Htmx/Htmx.php, line 1268

Class

Htmx
Presents the HTMX controls for developers to use with render arrays.

Namespace

Drupal\Core\Htmx

Code

public function applyTo(array &$element, string $attributeKey = '#attributes') : void {
  // Attach HTMX and Drupal integration javascript.
  if (!in_array('core/drupal.htmx', $element['#attached']['library'] ?? [])) {
    $element['#attached']['library'][] = 'core/drupal.htmx';
  }
  // Consolidate headers.
  if ($this->headers
    ->count() !== 0) {
    $element['#attached']['http_header'] = $element['#attached']['http_header'] ?? [];
    $element['#attached']['http_header'] = NestedArray::mergeDeep($element['#attached']['http_header'], $this->applyHeaders());
  }
  if (count($this->attributes
    ->storage()) !== 0) {
    // Consolidate attributes.
    $element[$attributeKey] = $element[$attributeKey] ?? [];
    $element[$attributeKey] = AttributeHelper::mergeCollections($element[$attributeKey], $this->attributes);
  }
  $this->cacheableMetadata
    ->applyTo($element);
}

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