Skip to content
Snippets Groups Projects

Upcast attributes in any variable

1 file
+ 10
6
Compare changes
  • Side-by-side
  • Inline
@@ -186,9 +186,12 @@ class ServerEndpointController extends ControllerBase {
private function generateRenderArray(Component $component, array $context): array {
$metadata = $component->metadata;
// Try to convert a key-value attributes property into the Drupal object.
if ($this->attributesPropNeedsUpcasting($context, $metadata)) {
$context['attributes'] = new Attribute($context['attributes']);
};
foreach ($context as $key => $value) {
if ($this->attributesPropNeedsUpcasting($context, $key, $metadata)) {
$context[$key] = new Attribute($value);
}
}
$block_names = array_keys($metadata->slots);
$slots = array_map(
static fn (string $slot_str) => [
@@ -218,13 +221,14 @@ class ServerEndpointController extends ControllerBase {
*
* @param array $context
* The template context.
* @param string $key
* @param \Drupal\sdc\Component\ComponentMetadata $metadata
* The component metadata.
*/
private function attributesPropNeedsUpcasting(array $context, ComponentMetadata $metadata) {
private function attributesPropNeedsUpcasting(array $context, string $key, ComponentMetadata $metadata) {
$properties = $metadata->schema['properties'] ?? [];
$context_has_attributes = is_array($context['attributes'] ?? NULL);
$metadata_has_attributes = ($properties['attributes']['type'][0] ?? NULL) === "Drupal\Core\Template\Attribute";
$context_has_attributes = is_array($context[$key] ?? NULL);
$metadata_has_attributes = ($properties[$key]['type'][0] ?? NULL) === "Drupal\Core\Template\Attribute";
return $context_has_attributes && $metadata_has_attributes;
}
Loading