function ComponentElement::preRenderComponent

Same name in this branch
  1. 10 core/modules/sdc/src/Element/ComponentElement.php \Drupal\sdc\Element\ComponentElement::preRenderComponent()
Same name in other branches
  1. 11.x core/modules/sdc/src/Element/ComponentElement.php \Drupal\sdc\Element\ComponentElement::preRenderComponent()
  2. 11.x core/lib/Drupal/Core/Render/Element/ComponentElement.php \Drupal\Core\Render\Element\ComponentElement::preRenderComponent()

Expands a component into an inline template with an attachment.

Parameters

array $element: The element to process. See main class documentation for properties.

Return value

array The form element.

Throws

\Drupal\Core\Render\Component\Exception\InvalidComponentDataException

File

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

Class

ComponentElement
Provides a Single-Directory Component render element.

Namespace

Drupal\Core\Render\Element

Code

public function preRenderComponent(array $element) : array {
    $props = $element['#props'];
    $props_alter_callbacks = $element['#propsAlter'];
    // This callback can be used to prepare the context. For instance to replace
    // tokens in the props.
    $props = array_reduce($props_alter_callbacks, fn(array $carry, callable $callback) => $this->doTrustedCallback($callback, [
        $carry,
    ], '%s is not trusted'), $props);
    $inline_template = $this->generateComponentTemplate($element['#component'], $element['#slots'], $element['#slotsAlter'], $props);
    $element['inline-template'] = [
        '#type' => 'inline_template',
        '#template' => $inline_template,
        '#context' => $props,
    ];
    return $element;
}

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