class ComponentElement
Same name in this branch
- 10 core/modules/sdc/src/Element/ComponentElement.php \Drupal\sdc\Element\ComponentElement
Same name in other branches
- 11.x core/modules/sdc/src/Element/ComponentElement.php \Drupal\sdc\Element\ComponentElement
- 11.x core/lib/Drupal/Core/Render/Element/ComponentElement.php \Drupal\Core\Render\Element\ComponentElement
Provides a Single-Directory Component render element.
Properties:
- #component: The machine name of the component.
- #props: an associative array where the keys are the names of the component props, and the values are the prop values.
- #slots: an associative array where the keys are the slot names, and the values are the slot values. Expected slot values are renderable arrays.
- #propsAlter: an array of trusted callbacks. These are used to prepare the context. Typical uses include replacing tokens in props.
- #slotsAlter: an array of trusted callbacks to alter the render array in #slots.
Usage Example:
$build['component'] = [
'#type' => 'component',
'#component' => 'olivero:button',
];
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Render\Element\RenderElementBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Render\Element\ElementInterface
- class \Drupal\Core\Render\Element\ComponentElement extends \Drupal\Core\Render\Element\RenderElementBase uses \Drupal\Core\Security\DoTrustedCallbackTrait
- class \Drupal\Core\Render\Element\RenderElementBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Render\Element\ElementInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of ComponentElement
See also
\Drupal\Core\Render\Element\Textarea
File
-
core/
lib/ Drupal/ Core/ Render/ Element/ ComponentElement.php, line 35
Namespace
Drupal\Core\Render\ElementView source
class ComponentElement extends RenderElementBase {
use DoTrustedCallbackTrait;
/**
* Expands a component into an inline template with an attachment.
*
* @param array $element
* The element to process. See main class documentation for properties.
*
* @return array
* The form element.
*
* @throws \Drupal\Core\Render\Component\Exception\InvalidComponentDataException
*/
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;
}
/**
* Generates the template to render the component.
*
* @param string $id
* The component id.
* @param array $slots
* The contents of any potential embed blocks.
* @param array $slots_alter_callbacks
* The potential callables for altering slots.
* @param array $context
* Inline template context.
*
* @return string
* The template.
*
* @throws \Drupal\Core\Render\Component\Exception\InvalidComponentDataException
* When slots are not render arrays.
*/
private function generateComponentTemplate(string $id, array $slots, array $slots_alter_callbacks, array &$context) : string {
$template = '{# This template was dynamically generated by single-directory components #}' . PHP_EOL;
$template .= sprintf('{%% embed \'%s\' %%}', $id);
$template .= PHP_EOL;
foreach ($slots as $slot_name => $slot_value) {
if (\is_scalar($slot_value)) {
$slot_value = [
"#plain_text" => (string) $slot_value,
];
}
if (!Element::isRenderArray($slot_value)) {
$message = sprintf('Unable to render component "%s". A render array or a scalar is expected for the slot "%s" when using the render element with the "#slots" property', $id, $slot_name);
throw new InvalidComponentDataException($message);
}
$context[$slot_name] = array_reduce($slots_alter_callbacks, fn(array $carry, callable $callback) => $this->doTrustedCallback($callback, [
$carry,
$context,
], '%s is not trusted'), $slot_value);
$template .= " {% block {$slot_name} %}" . PHP_EOL . " {{ {$slot_name} }}" . PHP_EOL . " {% endblock %}" . PHP_EOL;
}
$template .= '{% endembed %}' . PHP_EOL;
return $template;
}
/**
* {@inheritdoc}
*/
public function getInfo() : array {
return [
'#pre_render' => [
[
$this,
'preRenderComponent',
],
],
'#component' => '',
'#props' => [],
'#slots' => [],
'#propsAlter' => [],
'#slotsAlter' => [],
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
ComponentElement::generateComponentTemplate | private | function | Generates the template to render the component. | ||
ComponentElement::getInfo | public | function | Returns the element properties for this element. | Overrides ElementInterface::getInfo | |
ComponentElement::preRenderComponent | public | function | Expands a component into an inline template with an attachment. | ||
DoTrustedCallbackTrait::doTrustedCallback | public | function | Performs a callback. | ||
PluginInspectionInterface::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | 6 | |
PluginInspectionInterface::getPluginId | public | function | Gets the plugin ID of the plugin instance. | 2 | |
RenderElementBase::preRenderAjaxForm | public static | function | Adds Ajax information about an element to communicate with JavaScript. | 2 | |
RenderElementBase::preRenderGroup | public static | function | Adds members of this group as actual elements for rendering. | 2 | |
RenderElementBase::processAjaxForm | public static | function | Form element processing handler for the #ajax form property. | 3 | |
RenderElementBase::processGroup | public static | function | Arranges elements into groups. | 2 | |
RenderElementBase::setAttributes | public static | function | Sets a form element's class attribute. | Overrides ElementInterface::setAttributes | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.