-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperfish.theme.inc
271 lines (234 loc) · 9.96 KB
/
superfish.theme.inc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* @file
* Preprocessors and theme functions of the Superfish module.
*/
use Drupal\Core\Url;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\InaccessibleMenuLink;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\SafeMarkup;
/**
* Prepares variables for the Superfish menu template.
*
* Default template: superfish.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #menu_name, #html_id, #settings, #tree
* - menu_name: Unique menu identifier.
* - html_id: Unique HTML ID.
* - settings: Menu block settings.
* - tree: The menu tree.
*
* @see superfish.html.twig
*/
function template_preprocess_superfish(array &$variables) {
$element = $variables['element'];
$menu_items_rendered = array(
'#theme' => 'superfish_menu_items',
'#menu_name' => $element['#menu_name'],
'#tree' => $element['#tree'],
'#settings' => $element['#settings'],
'#cloned_parent' => false
);
$menu_classes = array('menu', 'sf-menu');
$menu_classes[] = 'sf-' . $element['#menu_name'];
$menu_classes[] = 'sf-' . $element['#settings']['menu_type'];
$menu_classes[] = 'sf-style-' . $element['#settings']['style'];
$menu_classes[] = (\Drupal::languageManager()->getCurrentLanguage()->getDirection() === LanguageInterface::DIRECTION_RTL) ? 'rtl' : '';
if (strpos($element['#settings']['ulclass'], ' ') !== FALSE) {
$l = explode(' ', $element['#settings']['ulclass']);
foreach ($l as $c) {
$menu_classes[] = Html::cleanCssIdentifier($c);
}
}
else {
$menu_classes[] = Html::cleanCssIdentifier($element['#settings']['ulclass']);
}
$menu_classes = implode(' ', superfish_array_remove_empty($menu_classes));
$variables['id'] = $element['#html_id'];
$variables['menu_classes'] = $menu_classes;
$variables['menu_items'] = $menu_items_rendered;
}
/**
* Prepares variables for Superfish menu items templates.
*
* Default template: superfish-menu-items.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #tree, #settings, #cloned_parent
* - tree: The menu tree.
* - menu_name: Unique menu identifier.
* - settings: Block settings
* - cloned_parent: Cloned sub-menu parent link.
*
* @see superfish-menu-items.html.twig
*/
function template_preprocess_superfish_menu_items(array &$variables) {
$element = $variables['element'];
// Keep $sfsettings untouched as we need to pass it to the child menus.
$settings = $sfsettings = $element['#settings'];
$multicolumn = $multicolumn_below = $settings['multicolumn'];
$variables['menu_items'] = array();
$menu = $element['#tree'];
// sfTouchscreen.
// Adding cloned parent to the sub-menu tree.
// Note, it is always false if it's not a sub-menu.
if ($element['#cloned_parent'] !== FALSE) {
array_unshift($menu, $element['#cloned_parent']);
}
$active_trails = \Drupal::service('menu.active_trail')->getActiveTrailIds($element['#menu_name']);
foreach ($menu as $key => $menu_item) {
if (null !== $menu_item->link && !($menu_item->link instanceof InaccessibleMenuLink)) {
$item_class = $link_class = array();
$multicolumn_wrapper = $multicolumn_column = $multicolumn_content = FALSE;
// Menu link properties.
$link = $menu_item->link->getPluginDefinition();
$item = array(
'id' => $link['id'],
'text' => $menu_item->link->getTitle(),
'description' => $menu_item->link->getDescription(),
'url' => $menu_item->link->getUrlObject(),
'enabled' => $link['enabled'],
'expanded' => $sfsettings['expanded'] ? $link['expanded'] : TRUE,
'options' => $link['options'],
'subtree' => $menu_item->subtree,
'depth' => $menu_item->depth,
'hasChildren' => $menu_item->hasChildren,
'inActiveTrail' => $menu_item->inActiveTrail
);
if ($menu_item->link->getUrlObject()->isRouted()) {
// Adding the "is-active" class.
$host = \Drupal::request()->getHttpHost();
$request_uri = \Drupal::request()->getRequestUri();
$current_url = Url::fromRoute('<current>');
$current_path = $current_url->toString();
$link_url = $item['url']->toString();
if ($link_url == $current_path || $link_url == $request_uri || $link_url == $host . $request_uri) {
$link_class[] = 'is-active';
}
}
// Adding the necessary "active-trail" class.
if ($item['inActiveTrail'] || array_key_exists($item['id'], $active_trails) || ($menu_item->link->getUrlObject()->isRouted() && $menu_item->link->getUrlObject()->getRouteName() == '<front>' && \Drupal::service('path.matcher')->isFrontPage())) {
$item_class[] = 'active-trail';
}
// Add menu link depth classes to the <li> element and its link.
$link_class[] = $settings['itemdepth'] ? 'sf-depth-' . $item['depth'] : '';
$item_class[] = $settings['itemdepth'] ? 'sf-depth-' . $item['depth'] : '';
// Indicates a cloned parent link, i.e. does not exist in the actual menu tree.
$item_class[] = $element['#cloned_parent'] ? 'sf-clone-parent' : '';
// Adding custom <li> classes.
if (strpos($settings['liclass'], ' ') !== FALSE) {
$l = explode(' ', $settings['liclass']);
foreach ($l as $c) {
$item_class[] = Html::cleanCssIdentifier($c);
}
}
else {
$item_class[] = Html::cleanCssIdentifier($settings['liclass']);
}
// Adding custom link classes.
if (strpos($settings['hlclass'], ' ') !== FALSE) {
$l = explode(' ', $settings['hlclass']);
foreach ($l as $c) {
$link_class[] = Html::cleanCssIdentifier($c);
}
}
else {
$link_class[] = Html::cleanCssIdentifier($settings['hlclass']);
}
// Add a class to external links.
$link_class[] = isset($item['options']['external']) ? 'sf-external' : '';
// Inserting link description (the "title" attribute) into the text.
if ($settings['add_linkdescription'] && !empty($item['description'])) {
$link_text = '@text <span class="sf-description">@description</span>';
$link_text_replace = array('@text' => $item['text'], '@description' => $item['description']);
} else {
$link_text = '@text';
$link_text_replace = array('@text' => $item['text']);
}
// Hiding link descriptions (the "title" attribute).
if ($settings['hide_linkdescription']) {
$item['options']['attributes']['title'] = '';
}
// Sub-menu.
if ($item['hasChildren'] && $item['subtree'] && $item['expanded']) {
// Multi-column sub-menus.
if ($settings['multicolumn']) {
$multicolumn_wrapper = ($item['depth'] == $settings['multicolumn_depth']) ? TRUE : FALSE;
$multicolumn_column = ($item['depth'] == $settings['multicolumn_depth'] + 1) ? TRUE : FALSE;
$multicolumn_content = ($item['depth'] >= $settings['multicolumn_depth'] && $item['depth'] <= $settings['multicolumn_levels']) ? TRUE : FALSE;
}
// sfTouchscreen.
// Preparing the cloned parent links to be added to the sub-menus.
if ($settings['clone_parent'] && $item['subtree']) {
$cloned_parent = $menu_item;
$cloned_parent->subtree = [];
}
else {
$cloned_parent = FALSE;
}
// Render the sub-menu.
$children = array(
'#theme' => 'superfish_menu_items',
'#menu_name' => $element['#menu_name'],
'#tree' => $item['subtree'],
'#settings' => $sfsettings,
'#cloned_parent' => $cloned_parent
);
if ($item['subtree']) {
// Adding some more classes.
$item_class[] = $multicolumn_column ? 'sf-multicolumn-column' : '';
$item_class[] = $link_class[] = 'menuparent';
}
}
else {
$children = '';
$item_class[] = 'sf-no-children';
}
// Preparing <li> classes for the theme.
$item_class = implode(' ', superfish_array_remove_empty($item_class));
// Merging link classes.
if (isset($item['options']['attributes']['class'])) {
$link_class_current = $item['options']['attributes']['class'];
if (!is_array($link_class_current)) {
$link_class_current = array($link_class_current);
}
$link_class = array_merge($link_class_current, superfish_array_remove_empty($link_class));
}
$item['options']['attributes']['class'] = superfish_array_remove_empty($link_class);
// Dirty fix! to only add a "menuparent" class.
$item['options_menuparent'] = $item['options'];
$item['options_menuparent']['attributes']['class'][] = 'menuparent';
$link_element = array(
'#type' => 'link',
'#title' => SafeMarkup::format($link_text, $link_text_replace),
'#url' => $item['url'],
'#options' => $item['options']
);
$link_element_menuparent = array(
'#type' => 'link',
'#title' => SafeMarkup::format($link_text, $link_text_replace),
'#url' => $item['url'],
'#options' => $item['options_menuparent']
);
$variables['menu_items'][] = array(
'id' => Html::getUniqueId($element['#menu_name'] .'-'. $item['id']),
'item_class' => $item_class,
'link' => $link_element,
'link_menuparent' => $link_element_menuparent,
'children' => $children,
'multicolumn_wrapper' => $multicolumn_wrapper,
'multicolumn_content' => $multicolumn_content,
'multicolumn_column' => $multicolumn_column
);
}
}
}