Skip to content
Snippets Groups Projects
Commit 15c2558a authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Issue #3473804 by just_like_good_vibes: Layout Builder: Restore icon_path &...

Issue #3473804 by just_like_good_vibes: Layout Builder: Restore icon_path & icon_map; add thumbnailPath
parent 4cc244ac
Branches
Tags
1 merge request!204fixed 3473804
Pipeline #281597 passed with warnings
......@@ -3,11 +3,15 @@
namespace Drupal\ui_patterns_layouts\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\ui_patterns\ComponentPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Filesystem\Path;
/**
* Provides layout plugin definitions for components.
......@@ -21,8 +25,12 @@ class ComponentLayout extends DeriverBase implements ContainerDeriverInterface {
*
* @param \Drupal\ui_patterns\ComponentPluginManager $pluginManager
* The component plugin manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
* The theme handler.
*/
public function __construct(protected ComponentPluginManager $pluginManager) {
public function __construct(protected ComponentPluginManager $pluginManager, protected ModuleHandlerInterface $moduleHandler, protected ThemeHandlerInterface $themeHandler) {
}
/**
......@@ -30,7 +38,9 @@ class ComponentLayout extends DeriverBase implements ContainerDeriverInterface {
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('plugin.manager.sdc')
$container->get('plugin.manager.sdc'),
$container->get('module_handler'),
$container->get('theme_handler'),
);
}
......@@ -40,6 +50,7 @@ class ComponentLayout extends DeriverBase implements ContainerDeriverInterface {
public function getDerivativeDefinitions($base_plugin_definition) {
$components = $this->pluginManager->getSortedDefinitions();
foreach ($components as $component) {
$component_instance = $this->pluginManager->find($component['id']);
/** @var \Drupal\Core\Layout\LayoutDefinition $base_plugin_definition */
$definition = array_merge([
"deriver" => $base_plugin_definition->getDeriver(),
......@@ -57,15 +68,75 @@ class ComponentLayout extends DeriverBase implements ContainerDeriverInterface {
"regions" => [],
]);
$layout_definition = new LayoutDefinition($definition);
if (isset($component['slots']) && is_array($component['slots']) && count($component['slots']) > 0) {
$regions = [];
foreach ($component['slots'] as $slot_id => $slot) {
$definition['regions'][$slot_id] = ['label' => $slot['title']];
$regions[$slot_id] = ['label' => $slot['title']];
}
$layout_definition->setRegions($regions);
$layout_definition->setDefaultRegion(array_key_first($regions));
}
if (isset($component['icon_map'])) {
$layout_definition->setIconMap($component['icon_map']);
}
$thumbnail_path = $component_instance->metadata->getThumbnailPath();
$layout_path = $this->getLayoutDefinitionPath($layout_definition);
if (!empty($thumbnail_path)) {
$layout_definition->setIconPath($this->getIconPath($thumbnail_path, $layout_path));
}
if (isset($component['icon_path'])) {
$layout_definition->setIconPath($this->getIconPath($component['icon_path'], $layout_path));
}
$id = str_replace('-', '_', (string) $component['id']);
$this->derivatives[$id] = new LayoutDefinition($definition);
$this->derivatives[$id] = $layout_definition;
}
return $this->derivatives;
}
/**
* Get Layout definition path.
*
* @param \Drupal\Core\Layout\LayoutDefinition $definition
* Layout definition.
*
* @return string
* Path.
*/
protected function getLayoutDefinitionPath(LayoutDefinition $definition) {
// Add the module or theme path to the 'path'.
$provider = $definition->getProvider();
if ($this->moduleHandler->moduleExists($provider)) {
$base_path = $this->moduleHandler->getModule($provider)->getPath();
}
elseif ($this->themeHandler->themeExists($provider)) {
$base_path = $this->themeHandler->getTheme($provider)->getPath();
}
else {
$base_path = '';
}
$path = $definition->getPath();
$path = !empty($path) ? $base_path . '/' . $path : $base_path;
return $path;
}
/**
* Get a layout icon path.
*
* @param string $icon_path
* Icon path as input.
* @param string $layout_path
* Path of the layout.
*
* @return string
* Path of the icon, relative to the layout path.
*/
protected function getIconPath(string $icon_path, string $layout_path) : string {
$base_path = base_path();
$layout_path = Path::makeAbsolute($layout_path, $base_path);
$path = (new SymfonyFilesystem())->makePathRelative(Path::makeAbsolute($icon_path, $base_path), $layout_path);
return str_ends_with($path, "/") ? substr($path, 0, strlen($path) - 1) : $path;
}
}
......@@ -8,7 +8,6 @@ use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\ui_patterns\Traits\RunSourcePluginTestTrait;
use Drupal\Tests\ui_patterns\Traits\TestContentCreationTrait;
use Drupal\Tests\ui_patterns\Traits\TestDataTrait;
/**
* Base class to test source plugins.
......@@ -18,7 +17,6 @@ use Drupal\Tests\ui_patterns\Traits\TestDataTrait;
class SourcePluginsTestBase extends KernelTestBase {
use RunSourcePluginTestTrait;
use TestDataTrait;
use TestContentCreationTrait;
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment