-
Notifications
You must be signed in to change notification settings - Fork 5
/
skilld_lang_dropdown.module
139 lines (122 loc) · 4.47 KB
/
skilld_lang_dropdown.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @file
* Primary module hooks for Language dropdown module.
*/
use Drupal\Core\Url;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Serialization\Json;
/**
* Implements hook_language_switch_links_alter().
*/
function skilld_lang_dropdown_language_switch_links_alter(
array &$links,
$type,
Url $url
) {
$is_error_page = \Drupal::request()->attributes->has('exception');
$current_langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
foreach ($links as $langcode => &$link) {
if ($is_error_page) {
$link['url'] = Url::fromRoute('<front>');
$link['query'] = [];
}
if ($current_langcode === $langcode) {
unset($link['url']);
$link['attributes']['class'][] = 'is-active';
}
}
}
/**
* Implements hook_theme().
*/
function skilld_lang_dropdown_theme($existing, $type, $theme, $path) {
return [
'dropdown_button' => [
'template' => 'dropdown-button',
'variables' => [
'attributes' => [],
'set_active_class' => FALSE,
'links' => [],
'button_icon' => '',
'button_text' => '',
],
],
];
}
function template_preprocess_dropdown_button(&$variables) {
$links = $variables['links'];
if (!empty($links)) {
$variables['links'] = [];
foreach ($links as $key => $link) {
$item = [];
$link += [
'ajax' => NULL,
'url' => NULL,
];
$li_attributes = new Attribute($link['attributes']);
$keys = ['title', 'url'];
$link_element = [
'#type' => 'link',
'#title' => $link['title'],
'#options' => array_diff_key($link, array_combine($keys, $keys)),
'#url' => $link['url'],
'#ajax' => $link['ajax'],
];
// Handle links and ensure that the active class is added on the LIs, but
// only if the 'set_active_class' option is not empty. Links templates
// duplicate the "is-active" class handling of l() and
// LinkGenerator::generate() because they need to be able to set the
// "is-active" class not on the links themselves (<a> tags), but on the
// list items (<li> tags) that contain the links. This is necessary for
// CSS to be able to style list items differently when the link is active,
// since CSS does not yet allow one to style list items only if they
// contain a certain element with a certain class. That is, we cannot yet
// convert this jQuery selector to a CSS selector:
// jQuery('li:has("a.is-active")')
if (isset($link['url'])) {
if (!empty($variables['set_active_class'])) {
// Also enable set_active_class for the contained link.
$link_element['#options']['set_active_class'] = TRUE;
if (!empty($link['language'])) {
$li_attributes['hreflang'] = $link['language']->getId();
}
// Add a "data-drupal-link-query" attribute to let the
// drupal.active-link library know the query in a standardized manner.
// Only add the data- attribute. The "is-active" class will be
// calculated using JavaScript, to prevent breaking the render cache.
if (!empty($link['query'])) {
$query = $link['query'];
ksort($query);
$li_attributes['data-drupal-link-query'] = Json::encode($query);
}
/** @var \Drupal\Core\Url $url */
$url = $link['url'];
if ($url->isRouted()) {
// Add a "data-drupal-link-system-path" attribute to let the
// drupal.active-link library know the path in a standardized
// manner. Only add the data- attribute. The "is-active" class will
// be calculated using JavaScript, to prevent breaking the render
// cache.
$system_path = $url->getInternalPath();
// @todo System path is deprecated - use the route name and parameters.
// Special case for the front page.
$li_attributes['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
}
}
$item['link'] = $link_element;
}
// Handle title-only text items.
$item['text'] = $link['title'];
if (isset($link['attributes'])) {
$item['text_attributes'] = new Attribute($link['attributes']);
}
// Handle list item attributes.
$item['attributes'] = new Attribute($li_attributes);
// Add the item to the list of links.
$variables['links'][$key] = $item;
}
}
}