Skip to content
Snippets Groups Projects
Commit 8165979a authored by Florent Torregrosa's avatar Florent Torregrosa
Browse files

Issue #3435933 by yassrzg, Grimreaper, pdureau: Introducing dependencies between themes

parent 915ff981
No related branches found
No related tags found
1 merge request!16Issue #3435933 by yassrzg Introducing dependencies between themes
Pipeline #210647 passed
......@@ -26,6 +26,7 @@ class ThemeDefinition extends PluginDefinition {
'library' => '',
'additional' => [],
'provider' => '',
'dependencies' => [],
];
/**
......@@ -241,6 +242,29 @@ class ThemeDefinition extends PluginDefinition {
return $this;
}
/**
* Getter.
*
* @return array
* Property value.
*/
public function getDependencies(): array {
return $this->definition['dependencies'];
}
/**
* Setter.
*
* @param array $dependencies
* Property value.
*
* @return $this
*/
public function setDependencies(array $dependencies) {
$this->definition['dependencies'] = $dependencies;
return $this;
}
/**
* Return array definition.
*
......
......@@ -48,31 +48,28 @@ class PreprocessHtml implements ContainerInjectionInterface {
return;
}
/** @var \Drupal\ui_skins\Definition\ThemeDefinition $plugin_definition */
$plugin_definition = $this->themePluginManager->getDefinition($ui_skins_theme_setting, FALSE);
if ($plugin_definition == NULL) {
return;
}
$definitions = $this->themePluginManager->getDefinitionWithDependencies($ui_skins_theme_setting);
foreach ($definitions as $definition) {
$target = $definition->getComputedTarget();
$key = $definition->getKey();
$value = $definition->getValue();
if ($key == 'class') {
$value = [
Html::getClass($value),
];
}
$target = $plugin_definition->getComputedTarget();
$key = $plugin_definition->getKey();
$value = $plugin_definition->getValue();
if ($key == 'class') {
$value = [
Html::getClass($value),
];
}
$variables[$target] = AttributeHelper::mergeCollections(
$variables[$target],
[
$key => $value,
]
);
$variables[$target] = AttributeHelper::mergeCollections(
$variables[$target] ?? [],
[
$key => $value,
]
);
$library = $plugin_definition->getLibrary();
if (!empty($library)) {
$variables['#attached']['library'][] = $library;
$library = $definition->getLibrary();
if (!empty($library)) {
$variables['#attached']['library'][] = $library;
}
}
}
......
......@@ -53,6 +53,7 @@ class ThemePluginManager extends DefaultPluginManager implements ThemePluginMana
'key' => 'class',
'value' => '',
'library' => '',
'dependencies' => [],
];
}
......@@ -121,4 +122,26 @@ class ThemePluginManager extends DefaultPluginManager implements ThemePluginMana
return $definitions;
}
/**
* {@inheritdoc}
*/
public function getDefinitionWithDependencies(string $pluginId): array {
$definitions = [];
$definition = $this->getDefinition($pluginId, FALSE);
if ($definition == NULL) {
return $definitions;
}
// Handle dependencies.
$dependencies = $definition->getDependencies();
foreach ($dependencies as $dependency) {
$definitions = \array_merge($definitions, $this->getDefinitionWithDependencies($dependency));
}
// Add our initial target last.
$definitions[] = $definition;
return $definitions;
}
}
......@@ -40,4 +40,15 @@ interface ThemePluginManagerInterface extends PluginManagerInterface {
*/
public function getDefinitionsForTheme(string $theme): array;
/**
* Get the plugin definition with its dependencies.
*
* @param string $pluginId
* A plugin id.
*
* @return \Drupal\ui_skins\Definition\ThemeDefinition[]
* The plugin definitions.
*/
public function getDefinitionWithDependencies(string $pluginId): array;
}
......@@ -28,6 +28,7 @@ class DummyThemePluginManager extends ThemePluginManager {
foreach ($definitions as $plugin_id => &$definition) {
$this->processDefinition($definition, $plugin_id);
}
$this->definitions = $definitions;
return $definitions;
}
......
......@@ -198,6 +198,13 @@ class ThemeTest extends UiSkinsFunctionalTestBase {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->elementExists('css', 'body.test-7');
$this->assertSession()->responseContains('href="' . Url::fromUserInput('/' . $ui_skins_test_themes_path . '/assets/css/test.css')->toString());
\drupal_flush_all_caches();
$this->setTheme('test_8');
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->elementExists('css', 'body.test-7.test-8');
$this->assertSession()->responseContains('href="' . Url::fromUserInput('/' . $ui_skins_test_themes_path . '/assets/css/test.css')->toString());
$this->assertSession()->responseContains('href="' . Url::fromUserInput('/' . $ui_skins_test_themes_path . '/assets/css/test_8.css')->toString());
}
/**
......
......@@ -116,4 +116,70 @@ class ThemePluginManagerTest extends UnitTestCase {
$this->assertEquals($definition->toArray(), $expected->toArray());
}
/**
* Tests the getDefinitionWithDependencies().
*
* @covers ::getDefinitionWithDependencies
*/
public function testGetDefinitionWithDependencies(): void {
$themes = [
'no_dependencies' => [
'id' => 'no_dependencies',
'label' => 'No dependencies',
'value' => 'foo',
],
'with_dependencies' => [
'id' => 'with_dependencies',
'label' => 'With dependencies',
'value' => 'foo',
'dependencies' => [
'dependency_1',
'dependency_2',
],
],
'dependency_1' => [
'id' => 'dependency_1',
'label' => 'Dependency 1',
'value' => 'foo',
'dependencies' => [
'dependency_1_1',
],
],
'dependency_1_1' => [
'id' => 'dependency_1_1',
'label' => 'Dependency 1 1',
'value' => 'foo',
],
'dependency_2' => [
'id' => 'dependency_2',
'label' => 'Dependency 2',
'value' => 'foo',
],
];
$this->themePluginManager->setThemes($themes);
$definitions = $this->themePluginManager->getDefinitionWithDependencies('no_dependencies');
$this->assertCount(1, $definitions);
$this->assertInstanceOf(ThemeDefinition::class, $definitions[0]);
$this->assertSame('No dependencies', $definitions[0]->getLabel());
$definitions = $this->themePluginManager->getDefinitionWithDependencies('dependency_1');
$this->assertCount(2, $definitions);
$this->assertInstanceOf(ThemeDefinition::class, $definitions[0]);
$this->assertSame('Dependency 1 1', $definitions[0]->getLabel());
$this->assertInstanceOf(ThemeDefinition::class, $definitions[1]);
$this->assertSame('Dependency 1', $definitions[1]->getLabel());
$definitions = $this->themePluginManager->getDefinitionWithDependencies('with_dependencies');
$this->assertCount(4, $definitions);
$this->assertInstanceOf(ThemeDefinition::class, $definitions[0]);
$this->assertSame('Dependency 1 1', $definitions[0]->getLabel());
$this->assertInstanceOf(ThemeDefinition::class, $definitions[1]);
$this->assertSame('Dependency 1', $definitions[1]->getLabel());
$this->assertInstanceOf(ThemeDefinition::class, $definitions[2]);
$this->assertSame('Dependency 2', $definitions[2]->getLabel());
$this->assertInstanceOf(ThemeDefinition::class, $definitions[3]);
$this->assertSame('With dependencies', $definitions[3]->getLabel());
}
}
.fake {
background-color: red;
}
......@@ -2,3 +2,8 @@ theme:
css:
theme:
assets/css/test.css: {}
test_8:
css:
theme:
assets/css/test_8.css: {}
......@@ -34,3 +34,11 @@ test_6:
test_7:
label: "(implicit) Body class with library"
library: "ui_skins_test_themes/theme"
# <body class="test-7 test-8">
# Test dependency.
test_8:
label: "Test dependency"
library: "ui_skins_test_themes/test_8"
dependencies:
- test_7
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