-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhome_redirect_lang.module
44 lines (36 loc) · 1.35 KB
/
home_redirect_lang.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
* @file
* Provides functionality for handling postal addresses.
*/
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\home_redirect_lang\HomeRedirectLangInterface;
/**
* Implements hook_preprocess_HOOK().
*
* Add both cache context based on cookie and referer for homepage.
*/
function home_redirect_lang_preprocess_page(&$variables): void {
if (!$variables['is_front']) {
return;
}
$variables['#cache']['contexts'][] = 'cookies:' . HomeRedirectLangInterface::COOKIE_PREFERRED_LANGCODE;
$variables['#cache']['contexts'][] = 'headers:REFERER';
$browser_config = \Drupal::configFactory()->get('home_redirect_lang.browser_fallback');
// When the Fallback on browser language is enabled for the first visit.
if ($browser_config->get('enable_browser_fallback')) {
$variables['#cache']['contexts'][] = 'headers:' . HomeRedirectLangInterface::BROWSER_HTTP_HEADER_PREFERRED_LANGCODE;
}
}
/**
* Implements hook_block_view_alter().
*
* Inject JavaScript switcher handler of the Homepage Redirect Language module
* when the drupal language is rendered in any page.
*/
function home_redirect_lang_block_view_alter(array &$build, BlockPluginInterface $block): void {
if ($block->getPluginId() !== 'language_block:language_interface') {
return;
}
$build['#attached']['library'][] = 'home_redirect_lang/language_switcher';
}