25
25
*/
26
26
class HomepageBrowserLanguageRedirection implements EventSubscriberInterface {
27
27
28
+ /**
29
+ * The Browser Redirection must be triggered before the Cookie redirection.
30
+ *
31
+ * The value here must be higher than
32
+ * {@HomepageCookieLanguageRedirection::PRIORITY}.
33
+ *
34
+ * This needs to run after \Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest(),
35
+ * which has a priority of 32.
36
+ * This needs to run before \Drupal\home_redirect_lang\EventSubscriber\HomepageCookieLanguageRedirection::redirectPreferredLanguage(),
37
+ * which has a priority of 30.
38
+ *
39
+ * @var int
40
+ */
41
+ private const PRIORITY = 31 ;
42
+
28
43
/**
29
44
* Symfony\Component\HttpFoundation\RequestStack definition.
30
45
*
@@ -66,9 +81,9 @@ public function __construct(RequestStack $request_stack, PathMatcher $path_match
66
81
/**
67
82
* {@inheritdoc}
68
83
*/
69
- public static function getSubscribedEvents () {
84
+ public static function getSubscribedEvents (): array {
70
85
return [
71
- KernelEvents::REQUEST => ['redirectPreferredLanguage ' ],
86
+ KernelEvents::REQUEST => ['redirectPreferredLanguage ' , self :: PRIORITY ],
72
87
];
73
88
}
74
89
@@ -78,7 +93,7 @@ public static function getSubscribedEvents() {
78
93
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
79
94
* The event.
80
95
*/
81
- public function redirectPreferredLanguage (RequestEvent $ event ) {
96
+ public function redirectPreferredLanguage (RequestEvent $ event ): void {
82
97
if (!$ this ->pathMatcher ->isFrontPage ()) {
83
98
return ;
84
99
}
@@ -97,14 +112,14 @@ public function redirectPreferredLanguage(RequestEvent $event) {
97
112
return ;
98
113
}
99
114
100
- // Whether or not preventing redirection when Referer Header is given.
115
+ // Whether preventing redirection when Referer Header is given.
101
116
$ referer_bypass_enabled = (bool ) $ this ->configFactory ->get ('home_redirect_lang.browser_fallback ' )->get ('enable_referer_bypass ' );
102
117
103
118
/** @var \Drupal\Core\Language\Language $current_language */
104
119
$ current_language = $ this ->languageManager ->getCurrentLanguage ();
105
120
$ http_referer = $ this ->request ->server ->get ('HTTP_REFERER ' );
106
121
$ current_host = $ this ->request ->getHost ();
107
- $ referer_host = parse_url ($ http_referer , \PHP_URL_HOST );
122
+ $ referer_host = parse_url (( string ) $ http_referer , \PHP_URL_HOST );
108
123
109
124
// Ensure the REFERER is external to disable redirection.
110
125
if ($ referer_bypass_enabled && !empty ($ referer_host ) && !empty ($ current_host ) && $ current_host !== $ referer_host ) {
@@ -113,7 +128,7 @@ public function redirectPreferredLanguage(RequestEvent $event) {
113
128
114
129
// When the preferred language cookie exists, then use it instead of the
115
130
// browser fallback.
116
- if ($ this ->request ->cookies ->has (HomeRedirectLangInterface::COOKIE_PREFERRED_LANGCODE )) {
131
+ if ($ this ->request ->cookies ->has (HomeRedirectLangInterface::COOKIE_PREFERRED_LANGCODE ) && $ this -> request -> cookies -> get (HomeRedirectLangInterface:: COOKIE_PREFERRED_LANGCODE ) !== '' ) {
117
132
return ;
118
133
}
119
134
@@ -126,7 +141,7 @@ public function redirectPreferredLanguage(RequestEvent $event) {
126
141
return ;
127
142
}
128
143
129
- // Ensure the stored langcode on the cookie is supported by Drupal.
144
+ // Ensure the stored langcode on the browser is supported by Drupal.
130
145
/** @var \Drupal\Core\Language\Language|null $destination_language */
131
146
$ destination_language = $ this ->languageManager ->getLanguage ($ destination_langcode );
132
147
0 commit comments