5 * Preprocessors and theme functions of the Superfish module.
9 use Drupal\Core\Menu\InaccessibleMenuLink;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\Component\Utility\Html;
12 use Drupal\Component\Utility\SafeMarkup;
15 * Prepares variables for the Superfish menu template.
17 * Default template: superfish.html.twig.
19 * @param array $variables
20 * An associative array containing:
21 * - element: An associative array containing the properties of the element.
22 * Properties used: #menu_name, #html_id, #settings, #tree
23 * - menu_name: Unique menu identifier.
24 * - html_id: Unique HTML ID.
25 * - settings: Menu block settings.
26 * - tree: The menu tree.
28 * @see superfish.html.twig
30 function template_preprocess_superfish(array &$variables) {
31 $element = $variables['element'];
33 $menu_items_rendered = [
34 '#theme' => 'superfish_menu_items',
35 '#menu_name' => $element['#menu_name'],
36 '#tree' => $element['#tree'],
37 '#settings' => $element['#settings'],
38 '#cloned_parent' => FALSE,
41 $direction = \Drupal::languageManager()->getCurrentLanguage()->getDirection();
42 $menu_classes = ['menu', 'sf-menu'];
43 $menu_classes[] = 'sf-' . $element['#menu_name'];
44 $menu_classes[] = 'sf-' . $element['#settings']['menu_type'];
45 $menu_classes[] = 'sf-style-' . $element['#settings']['style'];
47 $menu_classes[] = ($direction === LanguageInterface::DIRECTION_RTL) ? 'rtl' : '';
48 if (strpos($element['#settings']['ulclass'], ' ') !== FALSE) {
49 $l = explode(' ', $element['#settings']['ulclass']);
51 $menu_classes[] = Html::cleanCssIdentifier($c);
55 $menu_classes[] = Html::cleanCssIdentifier($element['#settings']['ulclass']);
57 $menu_classes = implode(' ', sf_array_filter($menu_classes));
59 $variables['id'] = $element['#html_id'];
60 $variables['menu_classes'] = $menu_classes;
61 $variables['menu_items'] = $menu_items_rendered;
66 * Prepares variables for Superfish menu items templates.
68 * Default template: superfish-menu-items.html.twig.
70 * @param array $variables
71 * An associative array containing:
72 * - element: An associative array containing the properties of the element.
73 * Properties used: #tree, #settings, #cloned_parent
74 * - tree: The menu tree.
75 * - menu_name: Unique menu identifier.
76 * - settings: Block settings
77 * - cloned_parent: Cloned sub-menu parent link.
79 * @see superfish-menu-items.html.twig
81 function template_preprocess_superfish_menu_items(array &$variables) {
83 $element = $variables['element'];
85 // Keep $sfsettings untouched as we need to pass it to the child menus.
86 $settings = $sfsettings = $element['#settings'];
87 $multicolumn = $multicolumn_below = $settings['multicolumn'];
89 $variables['menu_items'] = [];
91 $menu = $element['#tree'];
94 // Adding cloned parent to the sub-menu tree.
95 // Note, it is always false if it's not a sub-menu.
96 if ($element['#cloned_parent'] !== FALSE) {
97 array_unshift($menu, $element['#cloned_parent']);
100 $active_trails = \Drupal::service('menu.active_trail')
101 ->getActiveTrailIds($element['#menu_name']);
103 foreach ($menu as $key => $menu_item) {
105 if (NULL !== $menu_item->link &&
106 !($menu_item->link instanceof InaccessibleMenuLink)) {
108 $item_class = $link_class = [];
109 $multicolumn_wrapper = $multicolumn_column = $multicolumn_content = FALSE;
111 // Menu link properties.
112 $link = $menu_item->link->getPluginDefinition();
116 'text' => $menu_item->link->getTitle(),
117 'description' => $menu_item->link->getDescription(),
118 'url' => $menu_item->link->getUrlObject(),
119 'enabled' => $link['enabled'],
120 'expanded' => $sfsettings['expanded'] ? $link['expanded'] : TRUE,
121 'options' => $link['options'],
122 'subtree' => $menu_item->subtree,
123 'depth' => $menu_item->depth,
124 'hasChildren' => $menu_item->hasChildren,
125 'inActiveTrail' => $menu_item->inActiveTrail,
128 if ($menu_item->link->getUrlObject()->isRouted()) {
129 // Adding the "is-active" class.
130 $host = \Drupal::request()->getHttpHost();
131 $request_uri = \Drupal::request()->getRequestUri();
132 $current_url = Url::fromRoute('<current>');
133 $current_path = $current_url->toString();
134 $link_url = $item['url']->toString();
135 if ($link_url == $current_path || $link_url == $request_uri ||
136 $link_url == $host . $request_uri) {
137 $link_class[] = 'is-active';
141 // Adding the necessary "active-trail" class.
142 if ($item['inActiveTrail'] ||
143 array_key_exists($item['id'], $active_trails) ||
144 ($menu_item->link->getUrlObject()->isRouted() &&
145 $menu_item->link->getUrlObject()->getRouteName() == '<front>' &&
146 \Drupal::service('path.matcher')->isFrontPage())) {
147 $item_class[] = 'active-trail';
150 // Add menu link depth classes to the <li> element and its link.
151 if ($settings['itemdepth']) {
152 $link_class[] = 'sf-depth-' . $item['depth'];
153 $item_class[] = 'sf-depth-' . $item['depth'];
155 // Indicates a cloned parent, i.e. does not exist in the actual menu tree.
156 $item_class[] = $element['#cloned_parent'] ? 'sf-clone-parent' : '';
158 // Adding custom <li> classes.
159 if (strpos($settings['liclass'], ' ') !== FALSE) {
160 $l = explode(' ', $settings['liclass']);
162 $item_class[] = Html::cleanCssIdentifier($c);
166 $item_class[] = Html::cleanCssIdentifier($settings['liclass']);
169 // Adding custom link classes.
170 if (strpos($settings['hlclass'], ' ') !== FALSE) {
171 $l = explode(' ', $settings['hlclass']);
173 $link_class[] = Html::cleanCssIdentifier($c);
177 $link_class[] = Html::cleanCssIdentifier($settings['hlclass']);
180 // Add a class to external links.
181 $link_class[] = isset($item['options']['external']) ? 'sf-external' : '';
183 // Inserting link description (the "title" attribute) into the text.
184 if ($settings['add_linkdescription'] && !empty($item['description'])) {
185 $link_text = '@text <span class="sf-description">@description</span>';
186 $link_text_replace = [
187 '@text' => $item['text'],
188 '@description' => $item['description'],
192 $link_text = '@text';
193 $link_text_replace = [
194 '@text' => $item['text'],
198 // Hiding link descriptions (the "title" attribute).
199 if ($settings['hide_linkdescription']) {
200 $item['options']['attributes']['title'] = '';
204 if ($item['hasChildren'] && $item['subtree'] && $item['expanded']) {
206 // Multi-column sub-menus.
207 if ($settings['multicolumn']) {
208 if ($item['depth'] == $settings['multicolumn_depth']) {
209 $multicolumn_wrapper = TRUE;
212 $multicolumn_wrapper = FALSE;
214 if ($item['depth'] == $settings['multicolumn_depth'] + 1) {
215 $multicolumn_column = TRUE;
218 $multicolumn_column = FALSE;
220 if ($item['depth'] >= $settings['multicolumn_depth'] &&
221 $item['depth'] <= $settings['multicolumn_levels']) {
222 $multicolumn_content = TRUE;
225 $multicolumn_content = FALSE;
230 // Preparing the cloned parent links to be added to the sub-menus.
231 if ($settings['clone_parent'] && $item['subtree']) {
232 $cloned_parent = $menu_item;
233 $cloned_parent->subtree = [];
236 $cloned_parent = FALSE;
239 // Render the sub-menu.
241 '#theme' => 'superfish_menu_items',
242 '#menu_name' => $element['#menu_name'],
243 '#tree' => $item['subtree'],
244 '#settings' => $sfsettings,
245 '#cloned_parent' => $cloned_parent,
248 if ($item['subtree']) {
249 // Adding some more classes.
250 $item_class[] = $multicolumn_column ? 'sf-multicolumn-column' : '';
251 $item_class[] = $link_class[] = 'menuparent';
256 $item_class[] = 'sf-no-children';
259 // Preparing <li> classes for the theme.
260 $item_class = implode(' ', sf_array_filter($item_class));
262 // Merging link classes.
263 if (isset($item['options']['attributes']['class'])) {
264 $link_class_current = $item['options']['attributes']['class'];
265 if (!is_array($link_class_current)) {
266 $link_class_current = [$link_class_current];
268 $link_class = array_merge(
270 sf_array_filter($link_class)
273 $item['options']['attributes']['class'] = sf_array_filter($link_class);
275 // Dirty fix! to only add a "menuparent" class.
276 $item['options_menuparent'] = $item['options'];
277 $item['options_menuparent']['attributes']['class'][] = 'menuparent';
280 '#title' => SafeMarkup::format($link_text, $link_text_replace),
281 '#url' => $item['url'],
282 '#options' => $item['options'],
284 $link_element_menuparent = [
286 '#title' => SafeMarkup::format($link_text, $link_text_replace),
287 '#url' => $item['url'],
288 '#options' => $item['options_menuparent'],
291 $id = $element['#menu_name'] . '-' . $item['id'];
292 $variables['menu_items'][] = [
293 'id' => Html::getUniqueId($id),
294 'item_class' => $item_class,
295 'link' => $link_element,
296 'link_menuparent' => $link_element_menuparent,
297 'children' => $children,
298 'multicolumn_wrapper' => $multicolumn_wrapper,
299 'multicolumn_content' => $multicolumn_content,
300 'multicolumn_column' => $multicolumn_column,