-
Notifications
You must be signed in to change notification settings - Fork 1
/
cyrillic_to_latin.module
108 lines (97 loc) · 3.95 KB
/
cyrillic_to_latin.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* @file
* This is the primary module file.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\cyrillic_to_latin\CyrillicToLatinManager;
/**
* Implements hook_help().
*/
function cyrillic_to_latin_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.cyrillic_to_latin':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Converts cyrillic to latin letters on the fly for the Serbian language.') . '</p>';
return $output;
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function cyrillic_to_latin_preprocess_field(&$variables) {
$is_module_enabled = cyrillic_to_latin_is_module_enabled();
if ($is_module_enabled === FALSE) {
return;
}
$field_type = $variables['element']['#field_type'];
// Preprocess string fields.
if (in_array($field_type, ['string', 'string_long']) && isset($variables['items'][0]['content']['#context']['value'])) {
for ($i = 0; $i < count($variables['items']); $i++) {
$value = $variables['items'][$i]['content']['#context']['value'];
$variables['items'][$i]['content']['#context']['value'] = CyrillicToLatinManager::convertCyrillicToLatin($value);
}
}
// Preprocess text fields.
elseif (in_array($field_type, ['text', 'text_long', 'text_with_summary']) && isset($variables['items'][0]['content']['#text'])) {
for ($i = 0; $i < count($variables['items']); $i++) {
$value = $variables['items'][$i]['content']['#text'];
$variables['items'][$i]['content']['#text'] = CyrillicToLatinManager::convertCyrillicToLatin($value);
}
}
// Preprocess list text fields.
elseif ($field_type == 'list_string' && isset($variables['items'][0]['content']['#markup'])) {
for ($i = 0; $i < count($variables['items']); $i++) {
$value = $variables['items'][$i]['content']['#markup'];
$variables['items'][$i]['content']['#markup'] = CyrillicToLatinManager::convertCyrillicToLatin($value);
}
}
// Preprocess the plain address field formatter.
elseif ($field_type == 'address' && isset($variables['items'][0]['content']['#country']['name'])) {
for ($i = 0; $i < count($variables['items']); $i++) {
$country_name = $variables['items'][$i]['content']['#country']['name'];
$variables['items'][$i]['content']['#country']['name'] = CyrillicToLatinManager::convertCyrillicToLatin($country_name);
}
}
// Preprocess the default address field formatter.
elseif ($field_type == 'address' && isset($variables['items'][0]['content']['country']['#value'])) {
for ($i = 0; $i < count($variables['items']); $i++) {
$country_name = $variables['items'][$i]['content']['country']['#value'];
$variables['items'][$i]['content']['country']['#value'] = CyrillicToLatinManager::convertCyrillicToLatin($country_name);
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function cyrillic_to_latin_preprocess_views_view_field(&$variables) {
$is_module_enabled = cyrillic_to_latin_is_module_enabled();
if ($is_module_enabled === FALSE) {
return;
}
if ($variables['field']->field == 'address') {
$translated_address_output = CyrillicToLatinManager::convertCyrillicToLatin($variables['output']);
$variables['output'] = new TranslatableMarkup($translated_address_output);
}
}
/**
* Checks if the module is enabled.
*
* @return bool
* TRUE if the module is enabled, FALSE otherwise.
*/
function cyrillic_to_latin_is_module_enabled() {
/** @var \Drupal\Core\Config\ConfigFactory $config */
$config = \Drupal::configFactory()->get('cyrillic_to_latin.settings');
if (!$config->get('enabled')) {
return FALSE;
}
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$enabled_languages = !empty($config->get('languages')) ? array_filter(array_values($config->get('languages'))) : [];
if (!in_array($current_language, $enabled_languages)) {
return FALSE;
}
return TRUE;
}