Skip to content

Commit

Permalink
Merge pull request #1260 from openeuropa/EWPP-3180
Browse files Browse the repository at this point in the history
EWPP-3180: Fix translatability of field labels in FieldListPattern field_group formatter.
  • Loading branch information
nagyad authored Mar 20, 2023
2 parents 4af15a4 + 621c658 commit 7014a1a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"openeuropa/oe_corporate_blocks": "^4.11",
"openeuropa/oe_corporate_countries": "^2.0.0-alpha6",
"openeuropa/oe_media": "^1.22",
"openeuropa/oe_multilingual": "^1.13",
"openeuropa/oe_multilingual": "dev-master",
"openeuropa/oe_paragraphs": "^1.17",
"openeuropa/oe_search": "^1.9",
"openeuropa/oe_webtools": "^1.21",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ protected function getFields(array &$element, $rendering_object): array {
$fields = [];

foreach (Element::children($element) as $field_name) {
$label = $element[$field_name]['#title'] ?? '';
// By some conditions label of some fields could be passed not yet
// translated. It can be related to field_group implementation.
// @todo Investigate why some field labels are translated and some is not.
if (!empty($label) && is_string($label)) {
// @codingStandardsIgnoreStart
$label = $this->t($label);
// @codingStandardsIgnoreEnd
}
// Assign field label and content to the pattern's fields.
$fields['items'][] = [
'label' => $element[$field_name]['#title'] ?? '',
'label' => $label,
'body' => [
'#label_display' => 'hidden',
] + $element[$field_name],
Expand Down
4 changes: 4 additions & 0 deletions tests/src/Kernel/ContentLanguageSwitcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\node\Entity\Node;
use Drupal\Tests\oe_theme\Traits\RequestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Symfony\Component\DomCrawler\Crawler;

/**
Expand All @@ -16,6 +17,7 @@
class ContentLanguageSwitcherTest extends MultilingualAbstractKernelTestBase {

use RequestTrait;
use UserCreationTrait;

/**
* Modules to enable.
Expand All @@ -34,6 +36,7 @@ protected function setUp(): void {

$this->installEntitySchema('node');
$this->installSchema('node', 'node_access');
$this->container->get('current_user')->setAccount($this->createUser(['access content']));
}

/**
Expand All @@ -43,6 +46,7 @@ public function testLanguageSwitcherRendering(): void {
$node = Node::create([
'title' => 'Hello, world!',
'type' => 'oe_demo_translatable_page',
'status' => 1,
]);
/** @var \Drupal\Core\Entity\EntityInterface $translation */
$node->addTranslation('es', ['title' => '¡Hola mundo!'])->save();
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Kernel/MultilingualAbstractKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Drupal\Tests\oe_theme\Kernel;

use Drupal\Core\Routing\RouteObjectInterface;
use Drupal\locale\SourceString;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;

/**
* Base class for multilingual tests.
Expand Down Expand Up @@ -56,6 +59,13 @@ protected function setUp(): void {
$this->container->get('module_handler')->loadInclude('oe_multilingual', 'install');
oe_multilingual_install(FALSE);

// In order to make it possible correctly render the language switcher block
// we have to provide the current route explicitly.
$request = Request::create('/');
$request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$this->container->get('request_stack')->push($request);

// Rebuild the container in order to make sure tests pass.
// @todo fix test setup so that we can get rid of this line.
$this->container->get('kernel')->rebuildContainer();
Expand Down

0 comments on commit 7014a1a

Please sign in to comment.