Skip to content
Snippets Groups Projects

Issue #3369045: CL server unable to render the stories when storybook not installed at root of Drupal Installation

Open Issue #3369045: CL server unable to render the stories when storybook not installed at root of Drupal Installation
1 unresolved thread
Open Luca Lusso requested to merge issue/cl_server-3369045:3369045-cl-server-unable into 2.x
1 unresolved thread
1 file
+ 20
47
Compare changes
  • Side-by-side
  • Inline
@@ -4,7 +4,6 @@ namespace Drupal\cl_server\Controller;
@@ -4,7 +4,6 @@ namespace Drupal\cl_server\Controller;
use Drupal\sdc\ComponentPluginManager;
use Drupal\sdc\ComponentPluginManager;
use Drupal\sdc\Exception\ComponentNotFoundException;
use Drupal\sdc\Exception\ComponentNotFoundException;
use Drupal\sdc\Exception\TemplateNotFoundException;
use Drupal\sdc\Plugin\Component;
use Drupal\sdc\Plugin\Component;
use Drupal\sdc\Component\ComponentMetadata;
use Drupal\sdc\Component\ComponentMetadata;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\Attribute;
@@ -57,6 +56,7 @@ class ServerEndpointController extends ControllerBase {
@@ -57,6 +56,7 @@ class ServerEndpointController extends ControllerBase {
assert($cache_kill_switch instanceof KillSwitch);
assert($cache_kill_switch instanceof KillSwitch);
$plugin_manager = $container->get('plugin.manager.sdc');
$plugin_manager = $container->get('plugin.manager.sdc');
assert($plugin_manager instanceof ComponentPluginManager);
assert($plugin_manager instanceof ComponentPluginManager);
 
return new static($cache_kill_switch, $plugin_manager);
return new static($cache_kill_switch, $plugin_manager);
}
}
@@ -76,6 +76,7 @@ class ServerEndpointController extends ControllerBase {
@@ -76,6 +76,7 @@ class ServerEndpointController extends ControllerBase {
];
];
}
}
$this->cacheKillSwitch->trigger();
$this->cacheKillSwitch->trigger();
 
return [
return [
'#attached' => ['library' => ['cl_server/attach_behaviors']],
'#attached' => ['library' => ['cl_server/attach_behaviors']],
'#type' => 'container',
'#type' => 'container',
@@ -101,6 +102,7 @@ class ServerEndpointController extends ControllerBase {
@@ -101,6 +102,7 @@ class ServerEndpointController extends ControllerBase {
if ($json === FALSE) {
if ($json === FALSE) {
throw new BadRequestHttpException('Invalid component parameters');
throw new BadRequestHttpException('Invalid component parameters');
}
}
 
return Json::decode($json);
return Json::decode($json);
}
}
@@ -117,8 +119,19 @@ class ServerEndpointController extends ControllerBase {
@@ -117,8 +119,19 @@ class ServerEndpointController extends ControllerBase {
}
}
$basename = basename($story_filename);
$basename = basename($story_filename);
[$machine_name] = explode('.', $basename);
[$machine_name] = explode('.', $basename);
$provider = $this->findExtensionName($this->findStoryFile($story_filename));
return $this->pluginManager->createInstance("$provider:$machine_name");
$components = array_filter(
 
$this->pluginManager->getAllComponents(),
 
function (Component $component) use ($machine_name) {
 
return $component->machineName === $machine_name;
 
}
 
);
    • Comment on lines +123 to +128

      What if two components have the same machine name, but different provider (module or theme that contains the component)?

Please register or sign in to reply
 
 
if (count($components) === 0) {
 
throw new ComponentNotFoundException('Impossible to find a component with the machine name "' . $machine_name . '".');
 
}
 
 
return array_shift($components);
}
}
/**
/**
@@ -148,6 +161,7 @@ class ServerEndpointController extends ControllerBase {
@@ -148,6 +161,7 @@ class ServerEndpointController extends ControllerBase {
],
],
array_intersect_key($context, array_flip($block_names))
array_intersect_key($context, array_flip($block_names))
);
);
 
return [
return [
'#type' => 'component',
'#type' => 'component',
'#component' => $component->getPluginId(),
'#component' => $component->getPluginId(),
@@ -158,7 +172,7 @@ class ServerEndpointController extends ControllerBase {
@@ -158,7 +172,7 @@ class ServerEndpointController extends ControllerBase {
/**
/**
* Checks if the provided attributes need to be upcasted.
* Checks if the provided attributes need to be upcasted.
*
*
* Returns TRUE when the component library sends an associative
* Returns TRUE when the component library sends an associative
* array for the "attributes" property, and the metadata says
* array for the "attributes" property, and the metadata says
* it should be a Drupal\Core\Template\Attribute.
* it should be a Drupal\Core\Template\Attribute.
@@ -171,53 +185,12 @@ class ServerEndpointController extends ControllerBase {
@@ -171,53 +185,12 @@ class ServerEndpointController extends ControllerBase {
* @param \Drupal\sdc\Component\ComponentMetadata $metadata
* @param \Drupal\sdc\Component\ComponentMetadata $metadata
* The component metadata.
* The component metadata.
*/
*/
private function attributesPropNeedsUpcasting(array $context, ComponentMetadata $metadata) {
private function attributesPropNeedsUpcasting(array $context, ComponentMetadata $metadata): bool {
$properties = $metadata->schema['properties'] ?? [];
$properties = $metadata->schema['properties'] ?? [];
$context_has_attributes = is_array($context['attributes'] ?? NULL);
$context_has_attributes = is_array($context['attributes'] ?? NULL);
$metadata_has_attributes = ($properties['attributes']['type'][0] ?? NULL) === "Drupal\Core\Template\Attribute";
$metadata_has_attributes = ($properties['attributes']['type'][0] ?? NULL) === "Drupal\Core\Template\Attribute";
return $context_has_attributes && $metadata_has_attributes;
}
/**
return $context_has_attributes && $metadata_has_attributes;
* Finds the plugin ID from the story file name.
*
* The story file should be in the component directory, but storybook will
* not process is from the Drupal docroot. This means we don't know what the
* path is relative to.
*
* @param string $filename
* The filename.
*
* @return string
* The plugin ID.
*/
private function findStoryFile(string $filename): ?string {
if (empty($filename)) {
return NULL;
}
if (file_exists($filename)) {
return $filename;
}
$parts = explode(DIRECTORY_SEPARATOR, $filename);
array_shift($parts);
$filename = implode(DIRECTORY_SEPARATOR, $parts);
return $this->findStoryFile($filename);
}
/**
*
*/
private function findExtensionName(string $path): ?string {
if (empty($path)) {
return NULL;
}
$path = dirname($path);
$dir = basename($path);
$info_file = $path . DIRECTORY_SEPARATOR . "$dir.info.yml";
if (file_exists($info_file)) {
return $dir;
}
return $this->findExtensionName($path);
}
}
}
}
Loading