Unverified Commit 090bbaea authored by larowlan's avatar larowlan

Issue #3008772 by andypost, tim.plunkett: Layout definition defined in YAML is not translated

(cherry picked from commit 1b186cf0)
parent 6e29b513
......@@ -13,6 +13,7 @@
use Drupal\Core\Plugin\Discovery\YamlDiscoveryDecorator;
use Drupal\Core\Layout\Annotation\Layout;
use Drupal\Core\Plugin\FilteredPluginManagerTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides a plugin manager for layouts.
......@@ -71,6 +72,10 @@ protected function getDiscovery() {
if (!$this->discovery) {
$discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
$discovery = new YamlDiscoveryDecorator($discovery, 'layouts', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories());
$discovery
->addTranslatableProperty('label')
->addTranslatableProperty('description')
->addTranslatableProperty('category');
$discovery = new AnnotationBridgeDecorator($discovery, $this->pluginDefinitionAnnotationName);
$discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
$this->discovery = $discovery;
......@@ -140,6 +145,15 @@ public function processDefinition(&$definition, $plugin_id) {
if (!$definition->getDefaultRegion()) {
$definition->setDefaultRegion(key($definition->getRegions()));
}
// Makes sure region names are translatable.
$regions = array_map(function ($region) {
if (!$region['label'] instanceof TranslatableMarkup) {
// Region labels from YAML discovery needs translation.
$region['label'] = new TranslatableMarkup($region['label'], [], ['context' => 'layout_region']);
}
return $region;
}, $definition->getRegions());
$definition->setRegions($regions);
}
/**
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\Layout\LayoutPluginManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Prophecy\Argument;
......@@ -114,8 +115,12 @@ public function testGetDefinition() {
$theme_a_path = vfsStream::url('root/themes/theme_a');
$layout_definition = $this->layoutPluginManager->getDefinition('theme_a_provided_layout');
$this->assertSame('theme_a_provided_layout', $layout_definition->id());
$this->assertSame('2 column layout', $layout_definition->getLabel());
$this->assertSame('Columns: 2', $layout_definition->getCategory());
$this->assertSame('2 column layout', (string) $layout_definition->getLabel());
$this->assertSame('Columns: 2', (string) $layout_definition->getCategory());
$this->assertSame('A theme provided layout', (string) $layout_definition->getDescription());
$this->assertTrue($layout_definition->getLabel() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getCategory() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getDescription() instanceof TranslatableMarkup);
$this->assertSame('twocol', $layout_definition->getTemplate());
$this->assertSame("$theme_a_path/templates", $layout_definition->getPath());
$this->assertSame('theme_a/twocol', $layout_definition->getLibrary());
......@@ -126,19 +131,26 @@ public function testGetDefinition() {
$this->assertSame(LayoutDefault::class, $layout_definition->getClass());
$expected_regions = [
'left' => [
'label' => 'Left region',
'label' => new TranslatableMarkup('Left region', [], ['context' => 'layout_region']),
],
'right' => [
'label' => 'Right region',
'label' => new TranslatableMarkup('Right region', [], ['context' => 'layout_region']),
],
];
$this->assertSame($expected_regions, $layout_definition->getRegions());
$regions = $layout_definition->getRegions();
$this->assertEquals($expected_regions, $regions);
$this->assertTrue($regions['left']['label'] instanceof TranslatableMarkup);
$this->assertTrue($regions['right']['label'] instanceof TranslatableMarkup);
$module_a_path = vfsStream::url('root/modules/module_a');
$layout_definition = $this->layoutPluginManager->getDefinition('module_a_provided_layout');
$this->assertSame('module_a_provided_layout', $layout_definition->id());
$this->assertSame('1 column layout', $layout_definition->getLabel());
$this->assertSame('Columns: 1', $layout_definition->getCategory());
$this->assertSame('1 column layout', (string) $layout_definition->getLabel());
$this->assertSame('Columns: 1', (string) $layout_definition->getCategory());
$this->assertSame('A module provided layout', (string) $layout_definition->getDescription());
$this->assertTrue($layout_definition->getLabel() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getCategory() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getDescription() instanceof TranslatableMarkup);
$this->assertSame(NULL, $layout_definition->getTemplate());
$this->assertSame("$module_a_path/layouts", $layout_definition->getPath());
$this->assertSame('module_a/onecol', $layout_definition->getLibrary());
......@@ -149,19 +161,26 @@ public function testGetDefinition() {
$this->assertSame(LayoutDefault::class, $layout_definition->getClass());
$expected_regions = [
'top' => [
'label' => 'Top region',
'label' => new TranslatableMarkup('Top region', [], ['context' => 'layout_region']),
],
'bottom' => [
'label' => 'Bottom region',
'label' => new TranslatableMarkup('Bottom region', [], ['context' => 'layout_region']),
],
];
$this->assertSame($expected_regions, $layout_definition->getRegions());
$regions = $layout_definition->getRegions();
$this->assertEquals($expected_regions, $regions);
$this->assertTrue($regions['top']['label'] instanceof TranslatableMarkup);
$this->assertTrue($regions['bottom']['label'] instanceof TranslatableMarkup);
$core_path = '/core/lib/Drupal/Core';
$layout_definition = $this->layoutPluginManager->getDefinition('plugin_provided_layout');
$this->assertSame('plugin_provided_layout', $layout_definition->id());
$this->assertEquals('Layout plugin', $layout_definition->getLabel());
$this->assertEquals('Columns: 1', $layout_definition->getCategory());
$this->assertEquals('Test layout', $layout_definition->getDescription());
$this->assertTrue($layout_definition->getLabel() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getCategory() instanceof TranslatableMarkup);
$this->assertTrue($layout_definition->getDescription() instanceof TranslatableMarkup);
$this->assertSame('plugin-provided-layout', $layout_definition->getTemplate());
$this->assertSame($core_path, $layout_definition->getPath());
$this->assertSame(NULL, $layout_definition->getLibrary());
......@@ -172,10 +191,12 @@ public function testGetDefinition() {
$this->assertSame('Drupal\Core\Plugin\Layout\TestLayout', $layout_definition->getClass());
$expected_regions = [
'main' => [
'label' => 'Main Region',
'label' => new TranslatableMarkup('Main Region', [], ['context' => 'layout_region']),
],
];
$this->assertEquals($expected_regions, $layout_definition->getRegions());
$regions = $layout_definition->getRegions();
$this->assertEquals($expected_regions, $regions);
$this->assertTrue($regions['main']['label'] instanceof TranslatableMarkup);
}
/**
......@@ -284,6 +305,7 @@ protected function setUpFilesystem() {
module_a_provided_layout:
label: 1 column layout
category: 'Columns: 1'
description: 'A module provided layout'
theme_hook: onecol
path: layouts
library: module_a/onecol
......@@ -301,6 +323,7 @@ protected function setUpFilesystem() {
class: '\Drupal\Core\Layout\LayoutDefault'
label: 2 column layout
category: 'Columns: 2'
description: 'A theme provided layout'
template: twocol
path: templates
library: theme_a/twocol
......@@ -325,7 +348,7 @@ class: '\Drupal\Core\Layout\LayoutDefault'
* template = "templates/plugin-provided-layout",
* regions = {
* "main" = {
* "label" = @Translation("Main Region")
* "label" = @Translation("Main Region", context = "layout_region")
* }
* }
* )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment