Skip to content

Commit a6b43b0

Browse files
authored
Ensure libraries get attached when rendering smart components. (#35)
1 parent 213ab22 commit a6b43b0

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/PantheonTagsToRenderable.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ public function convertJsonToRenderable(string $json): array {
4242
$container->setAttribute('class', $uniqueClass);
4343

4444
$metadata = new CacheableMetadata();
45-
$this->processNode($node, $container, $uniqueClass, $metadata);
46-
$html = Html::serialize($domDocument);
47-
4845
$build = [
4946
'#type' => 'inline_template',
5047
'#template' => '{{ value | raw }}',
51-
'#context' => ['value' => $html],
48+
'#context' => ['value' => ''],
49+
'#attached' => [],
5250
];
51+
52+
$this->processNode($node, $container, $uniqueClass, $metadata, $build);
53+
$html = Html::serialize($domDocument);
54+
$build['#context']['value'] = $html;
5355
$metadata->applyTo($build);
5456

5557
return $build;
@@ -66,8 +68,10 @@ public function convertJsonToRenderable(string $json): array {
6668
* The unique class used for CSS scoping.
6769
* @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
6870
* The caching metadata.
71+
* @param array &$build
72+
* The top-level render array to merge attachments into (passed by reference).
6973
*/
70-
protected function processNode(array $node, \DOMElement $parent, string $uniqueClass, RefinableCacheableDependencyInterface $metadata): void {
74+
protected function processNode(array $node, \DOMElement $parent, string $uniqueClass, RefinableCacheableDependencyInterface $metadata, array &$build): void {
7175
$defaults = [
7276
'tag' => 'div',
7377
'data' => '',
@@ -93,8 +97,13 @@ protected function processNode(array $node, \DOMElement $parent, string $uniqueC
9397
$component = PantheonSmartInstance::create(['component' => $node['type']] + $attrs);
9498
// Changing the component should invalidate cache.
9599
$metadata->addCacheableDependency($component);
96-
$build = $this->viewBuilder->view($component);
97-
$html = (string) $this->renderer->renderInIsolation($build);
100+
$component_build = $this->viewBuilder->view($component);
101+
$html = (string) $this->renderer->renderInIsolation($component_build);
102+
103+
if (!empty($component_build['#attached'])) {
104+
$build['#attached'] = array_merge_recursive($build['#attached'], $component_build['#attached']);
105+
}
106+
98107
$element = $domDocument->importNode(Html::load($html)->documentElement, TRUE);
99108
$attrs = [];
100109
}
@@ -115,7 +124,7 @@ protected function processNode(array $node, \DOMElement $parent, string $uniqueC
115124
$element->setAttribute('style', implode('; ', $style));
116125
}
117126
foreach ($children as $child) {
118-
$this->processNode($child, $element, $uniqueClass, $metadata);
127+
$this->processNode($child, $element, $uniqueClass, $metadata, $build);
119128
}
120129
$parent->appendChild($element);
121130
}

0 commit comments

Comments
 (0)