22
33namespace Drupal \graphql_core \Plugin \GraphQL \Fields \LanguageSwitch ;
44
5+ use Drupal \Core \Config \ConfigFactoryInterface ;
56use Drupal \Core \Language \LanguageInterface ;
67use Drupal \Core \Language \LanguageManagerInterface ;
78use 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 ();
0 commit comments