function Htmx::createFromRenderArray
Creates an Htmx object with values taken from a render array.
Parameters
array $element: A render array.
string $attributeKey: Optional target key for attribute output: defaults to '#attributes'.
Return value
static A new instance of this class.
1 call to Htmx::createFromRenderArray()
- HtmxUtilitiesTest::testCreateFromRenderArray in core/
tests/ Drupal/ Tests/ Core/ Htmx/ HtmxUtilitiesTest.php - Test ::createFromRenderArray.
File
-
core/
lib/ Drupal/ Core/ Htmx/ Htmx.php, line 1298
Class
- Htmx
- Presents the HTMX controls for developers to use with render arrays.
Namespace
Drupal\Core\HtmxCode
public static function createFromRenderArray(array $element, string $attributeKey = '#attributes') : static {
$incomingAttributes = $element[$attributeKey] ?? [];
$incomingHeaders = $element['#attached']['http_header'] ?? [];
// Filter for HTMX values.
$incomingAttributes = array_filter($incomingAttributes, function (string $key) {
return str_starts_with($key, 'data-hx-');
}, ARRAY_FILTER_USE_KEY);
$preparedHeaders = [];
foreach ($incomingHeaders as $value) {
if (is_array($value) && str_starts_with($value[0], 'hx-')) {
// Header value array may have 3 values, we want the first two.
$preparedHeaders[$value[0]] = $value[1];
}
}
$attributes = new Attribute($incomingAttributes);
$headers = new HeaderBag($preparedHeaders);
$cacheableMetadata = CacheableMetadata::createFromRenderArray($element);
return new static($attributes, $headers, $cacheableMetadata);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.