Skip to content

Commit 4e82585

Browse files
pmelabfubhy
authored andcommitted
Use <frontpage> route for frontpage language switch links. (#663)
1 parent e95abdf commit 4e82585

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

modules/graphql_core/src/Plugin/GraphQL/Fields/LanguageSwitch/LanguageSwitchLinks.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\LanguageSwitch;
44

5+
use Drupal\Core\Config\ConfigFactoryInterface;
56
use Drupal\Core\Language\LanguageInterface;
67
use Drupal\Core\Language\LanguageManagerInterface;
78
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@@ -45,6 +46,13 @@ class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPlu
4546
*/
4647
protected $subRequestBuffer;
4748

49+
/**
50+
* The config factory.
51+
*
52+
* @var \Drupal\Core\Config\ConfigFactoryInterface
53+
*/
54+
protected $configFactory;
55+
4856
/**
4957
* {@inheritdoc}
5058
*/
@@ -54,7 +62,8 @@ public static function create(ContainerInterface $container, array $configuratio
5462
$plugin_id,
5563
$plugin_definition,
5664
$container->get('language_manager'),
57-
$container->get('graphql.buffer.subrequest')
65+
$container->get('graphql.buffer.subrequest'),
66+
$container->get('config.factory')
5867
);
5968
}
6069

@@ -66,11 +75,13 @@ public function __construct(
6675
$pluginId,
6776
$pluginDefinition,
6877
LanguageManagerInterface $languageManager,
69-
SubRequestBuffer $subRequestBuffer
78+
SubRequestBuffer $subRequestBuffer,
79+
ConfigFactoryInterface $configFactory
7080
) {
7181
parent::__construct($configuration, $pluginId, $pluginDefinition);
7282
$this->languageManager = $languageManager;
7383
$this->subRequestBuffer = $subRequestBuffer;
84+
$this->configFactory = $configFactory;
7485
}
7586

7687
/**
@@ -79,7 +90,14 @@ public function __construct(
7990
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
8091
if ($value instanceof Url) {
8192

93+
// Use the <front> route if the requested url is the frontpage.
94+
$frontpage = $this->configFactory->get('system.site')->get('page.front');
95+
if ('/' . $value->getInternalPath() === $frontpage) {
96+
$value = Url::fromRoute('<front>');
97+
}
98+
8299
$links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $value);
100+
83101
$current = $this->languageManager->getLanguage($args['language']);
84102
if (!$current) {
85103
$current = $this->languageManager->getDefaultLanguage();

modules/graphql_core/tests/src/Kernel/Languages/LanguageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function testLanguageSwitchLinks() {
116116
'id' => 'en',
117117
],
118118
'url' => [
119-
'path' => '/en/user/login',
119+
'path' => '/en',
120120
],
121121
'title' => 'English',
122122
'active' => TRUE,
@@ -126,7 +126,7 @@ public function testLanguageSwitchLinks() {
126126
'id' => 'fr',
127127
],
128128
'url' => [
129-
'path' => '/fr/user/login',
129+
'path' => '/fr',
130130
],
131131
'title' => NULL,
132132
'active' => FALSE,
@@ -136,7 +136,7 @@ public function testLanguageSwitchLinks() {
136136
'id' => 'es',
137137
],
138138
'url' => [
139-
'path' => '/es/user/login',
139+
'path' => '/es',
140140
],
141141
'title' => NULL,
142142
'active' => FALSE,
@@ -146,7 +146,7 @@ public function testLanguageSwitchLinks() {
146146
'id' => 'pt-br',
147147
],
148148
'url' => [
149-
'path' => '/user/login',
149+
'path' => '/',
150150
],
151151
'title' => NULL,
152152
'active' => FALSE,

0 commit comments

Comments
 (0)