Skip to content
Snippets Groups Projects
Commit b57df752 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Issue #3356150 by e0ipso: Support SDC in core

parent 514a44d2
No related branches found
Tags 2.0.0-alpha2
No related merge requests found
......@@ -5,6 +5,7 @@ namespace Drupal\cl_server\Asset;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\cl_server\Util;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
......@@ -41,20 +42,22 @@ class UnoptimizedAssetResolver implements AssetResolverInterface {
/**
* @inheritDoc
*/
public function getCssAssets(AttachedAssetsInterface $assets, $optimize) {
public function getCssAssets(AttachedAssetsInterface $assets, $optimize, LanguageInterface $language = NULL) {
return $this->resolver->getCssAssets(
$assets,
$this->skipOptimization ? FALSE : $optimize
$this->skipOptimization ? FALSE : $optimize,
$language
);
}
/**
* @inheritDoc
*/
public function getJsAssets(AttachedAssetsInterface $assets, $optimize) {
public function getJsAssets(AttachedAssetsInterface $assets, $optimize, LanguageInterface $language = NULL) {
return $this->resolver->getJsAssets(
$assets,
$this->skipOptimization ? FALSE : $optimize
$this->skipOptimization ? FALSE : $optimize,
$language
);
}
......
......@@ -62,16 +62,13 @@ class ServerEndpointController extends ControllerBase {
* Render a Twig template from a Storybook component directory.
*/
public function render(Request $request): array {
$arguments = $this->getArguments($request);
try {
$component = $this->getComponent($request);
$variant = $request->query->get('_variant');
// Storybook will not allow empty title for the story. Let's consider
// Default to be the default.
$variant = $variant === 'default' ? '' : $variant ?? '';
$build = $this->generateRenderArray($component, $variant ?: '', $arguments);
$build = $this->generateRenderArray(
$this->getComponent($request),
$this->getArguments($request)
);
}
catch (ComponentNotFoundException|TemplateNotFoundException $e) {
catch (ComponentNotFoundException $e) {
$build = [
'#markup' => '<div class="messages messages--error"><h3>' . $this->t('Unable to find component') . '</h3>' . $this->t('Check that the module or theme containing the component is enabled and matches the stories file name.') . '</div>',
];
......@@ -128,25 +125,28 @@ class ServerEndpointController extends ControllerBase {
*
* @param \Drupal\sdc\Plugin\Component $component
* The component.
* @param string $variant
* The variant.
* @param array $context
* The template context.
*
* @return array
* The generated render array.
*/
private function generateRenderArray(Component $component, string $variant, array $context): array {
$metadata = $component->getMetadata();
$component_schema = $metadata->getSchemas()['slots'] ?? [];
$block_names = array_keys($component_schema['properties'] ?? []);
private function generateRenderArray(Component $component, array $context): array {
$metadata = $component->metadata;
$block_names = array_keys($metadata->slots);
$slots = array_map(
static fn (string $slot_str) => [
'#type' => 'inline_template',
'#template' => $slot_str,
'#context' => $context,
],
array_intersect_key($context, array_flip($block_names))
);
return [
'#type' => 'sdc',
'#type' => 'component',
'#component' => $component->getPluginId(),
'#variant' => $variant,
'#slots' => array_intersect_key($context, array_flip($block_names)),
'#trusted_slots' => TRUE,
'#context' => array_diff_key($context, array_flip($block_names)),
'#slots' => $slots,
'#props' => array_diff_key($context, array_flip($block_names)),
];
}
......@@ -158,7 +158,7 @@ class ServerEndpointController extends ControllerBase {
* path is relative to.
*
* @param string $filename
* The file name.
* The filename.
*
* @return string
* The plugin ID.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment