Skip to content
Snippets Groups Projects

Issue #3462325 by pdureau, grimreaper: Allow library wrappers around stories

Merged Issue #3462325 by pdureau, grimreaper: Allow library wrappers around stories
All threads resolved!
All threads resolved!
Files
3
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns_library\Template;
use Drupal\ui_patterns_library\StoryPluginManager;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
@@ -14,6 +15,9 @@ use Twig\TwigFunction;
*/
class TwigExtension extends AbstractExtension {
public function __construct(protected StoryPluginManager $storyPluginManager) {
}
/**
* {@inheritdoc}
*/
@@ -44,20 +48,34 @@ class TwigExtension extends AbstractExtension {
* Component slots to override.
* @param array $props
* Component props to override.
* @param bool $with_wrapper
* Wrap the story into a markup.
*
* @return array
* Pattern render array.
*
* @see \Drupal\Core\Theme\Element\ComponentElement
*/
public function renderComponentStory(string $component_id, string $story_id, array $slots = [], array $props = []) {
return [
public function renderComponentStory(string $component_id, string $story_id, array $slots = [], array $props = [], bool $with_wrapper = FALSE) {
$renderable = [
'#type' => 'component',
'#component' => $component_id,
'#story' => $story_id,
'#slots' => $slots,
'#props' => $props,
];
if (!$with_wrapper) {
return $renderable;
}
$story = $this->storyPluginManager->getDefinition($component_id . ':' . $story_id);
if (!isset($story["library_wrapper"]) || empty($story["library_wrapper"])) {
return $renderable;
}
return [
'#type' => 'inline_template',
'#template' => $story["library_wrapper"],
'#context' => $slots + $props + ['_story' => $renderable],
];
}
}
Loading