-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_switcher_popup.module
More file actions
56 lines (47 loc) · 1.92 KB
/
Copy pathlanguage_switcher_popup.module
File metadata and controls
56 lines (47 loc) · 1.92 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* @file
* Language Switcher Popup module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function language_switcher_popup_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case "help.page.language_switcher_popup":
$output = "<h3>" . t("About") . "</h3>";
$output .= "<p>" . t("Shows a confirmation popup that search filters will be reset when users switch languages") . "</p>";
$output .= "<h3>" . t("Configuration") . "</h3>";
$output .= "<p>" . t('Configure the popup messages at <a href=":config">Language Switcher Popup Settings</a>.', [
":config" => Url::fromRoute("language_switcher_popup.settings")->toString(),
]) . "</p>";
return $output;
}
}
/**
* Implements hook_page_attachments().
*/
function language_switcher_popup_page_attachments(array &$attachments) {
// Attach the library to all pages.
$attachments["#attached"]["library"][] = "language_switcher_popup/language_switcher_popup";
// Pass configuration to JavaScript.
$config = \Drupal::config("language_switcher_popup.settings");
$language_manager = \Drupal::languageManager();
$languages = $language_manager->getLanguages();
// 1. Get the default site language code
$default_langcode = $language_manager->getDefaultLanguage()->getId();
$settings = [
"enabled" => (bool) ($config->get("enable_popup") ?? TRUE),
"defaultLanguage" => $default_langcode,
];
foreach ($languages as $langcode => $language) {
$settings[$langcode] = [
"message" => $config->get("message_{$langcode}") ?: t("Switching languages will reset your search filters. Do you want to continue?"),
"confirm" => $config->get("confirm_{$langcode}") ?: t("Continue"),
"cancel" => $config->get("cancel_{$langcode}") ?: t("Cancel"),
];
}
$attachments["#attached"]["drupalSettings"]["languageSwitcherPopup"] = $settings;
}