function ComponentElement::mergeElementAttributesToPropAttributes

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Render/Element/ComponentElement.php \Drupal\Core\Render\Element\ComponentElement::mergeElementAttributesToPropAttributes()

Merge element attributes with props attributes.

#attributes property is an universal property of the Render API, used by many Drupal mechanisms from Core and Contrib, so we need to inject the values in template.

Parameters

array $element: The render element.

1 call to ComponentElement::mergeElementAttributesToPropAttributes()
ComponentElement::preRenderComponent in core/lib/Drupal/Core/Render/Element/ComponentElement.php
Expands a component into an inline template with an attachment.

File

core/lib/Drupal/Core/Render/Element/ComponentElement.php, line 164

Class

ComponentElement
Provides a Single-Directory Component render element.

Namespace

Drupal\Core\Render\Element

Code

private function mergeElementAttributesToPropAttributes(array &$element) : void {
  // Prepare #props attributes to be both mergeable and renderable.
  $prop_attributes = $element['#props']['attributes'] ?? [];
  $prop_attributes = is_array($prop_attributes) ? new Attribute($prop_attributes) : $prop_attributes;
  if (!isset($element['#attributes'])) {
    $element['#props']['attributes'] = $prop_attributes;
    return;
  }
  // If attributes value is an array, convert it to an Attribute object as
  // \Drupal\Core\Template\Attribute::merge() expects an Attribute object.
  $element_attributes = is_array($element['#attributes']) ? new Attribute($element['#attributes']) : $element['#attributes'];
  // Merge ['#attributes'] with the ['#props']['attributes']. So that #props
  // attributes take precedence.
  $element['#props']['attributes'] = $element_attributes->merge($prop_attributes);
}

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