4 * Preprocessors and theme functions of the Superfish module.
8 use Drupal\Core\Menu\MenuLinkDefault;
9 use Drupal\Core\Menu\InaccessibleMenuLink;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\Core\Template\Attribute;
12 use Drupal\Component\Utility\Html;
13 use Drupal\Component\Utility\Unicode;
14 use Drupal\Component\Utility\SafeMarkup;
17 * Prepares variables for the Superfish menu template.
19 * Default template: superfish.html.twig.
21 * @param array $variables
22 * An associative array containing:
23 * - element: An associative array containing the properties of the element.
24 * Properties used: #menu_name, #html_id, #settings, #tree
25 * - menu_name: Unique menu identifier.
26 * - html_id: Unique HTML ID.
27 * - settings: Menu block settings.
28 * - tree: The menu tree.
30 * @see superfish.html.twig
33 function template_preprocess_superfish(array &$variables) {
34 $element = $variables['element'];
36 $menu_items_rendered = array(
37 '#theme' => 'superfish_menu_items',
38 '#menu_name' => $element['#menu_name'],
39 '#tree' => $element['#tree'],
40 '#settings' => $element['#settings'],
41 '#cloned_parent' => false
44 $menu_classes = array('menu', 'sf-menu');
45 $menu_classes[] = 'sf-' . $element['#menu_name'];
46 $menu_classes[] = 'sf-' . $element['#settings']['menu_type'];
47 $menu_classes[] = 'sf-style-' . $element['#settings']['style'];
48 $menu_classes[] = (\Drupal::languageManager()->getCurrentLanguage()->getDirection() === LanguageInterface::DIRECTION_RTL) ? 'rtl' : '';
49 if (strpos($element['#settings']['ulclass'], ' ') !== FALSE) {
50 $l = explode(' ', $element['#settings']['ulclass']);
52 $menu_classes[] = Html::cleanCssIdentifier($c);
56 $menu_classes[] = Html::cleanCssIdentifier($element['#settings']['ulclass']);
58 $menu_classes = implode(' ', superfish_array_remove_empty($menu_classes));
60 $variables['id'] = $element['#html_id'];
61 $variables['menu_classes'] = $menu_classes;
62 $variables['menu_items'] = $menu_items_rendered;
67 * Prepares variables for Superfish menu items templates.
69 * Default template: superfish-menu-items.html.twig.
71 * @param array $variables
72 * An associative array containing:
73 * - element: An associative array containing the properties of the element.
74 * Properties used: #tree, #settings, #cloned_parent
75 * - tree: The menu tree.
76 * - menu_name: Unique menu identifier.
77 * - settings: Block settings
78 * - cloned_parent: Cloned sub-menu parent link.
80 * @see superfish-menu-items.html.twig
83 function template_preprocess_superfish_menu_items(array &$variables) {
85 $element = $variables['element'];
87 // Keep $sfsettings untouched as we need to pass it to the child menus.
88 $settings = $sfsettings = $element['#settings'];
89 $multicolumn = $multicolumn_below = $settings['multicolumn'];
91 $variables['menu_items'] = array();
93 $menu = $element['#tree'];
96 // Adding cloned parent to the sub-menu tree.
97 // Note, it is always false if it's not a sub-menu.
98 if ($element['#cloned_parent'] !== FALSE) {
99 array_unshift($menu, $element['#cloned_parent']);
102 $active_trails = \Drupal::service('menu.active_trail')->getActiveTrailIds($element['#menu_name']);
104 foreach ($menu as $key => $menu_item) {
106 if (null !== $menu_item->link && !($menu_item->link instanceof InaccessibleMenuLink)) {
108 $item_class = $link_class = array();
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 || $link_url == $host . $request_uri) {
136 $link_class[] = 'is-active';
140 // Adding the necessary "active-trail" class.
141 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())) {
142 $item_class[] = 'active-trail';
145 // Add menu link depth classes to the <li> element and its link.
146 $link_class[] = $settings['itemdepth'] ? 'sf-depth-' . $item['depth'] : '';
147 $item_class[] = $settings['itemdepth'] ? 'sf-depth-' . $item['depth'] : '';
148 // Indicates a cloned parent link, i.e. does not exist in the actual menu tree.
149 $item_class[] = $element['#cloned_parent'] ? 'sf-clone-parent' : '';
151 // Adding custom <li> classes.
152 if (strpos($settings['liclass'], ' ') !== FALSE) {
153 $l = explode(' ', $settings['liclass']);
155 $item_class[] = Html::cleanCssIdentifier($c);
159 $item_class[] = Html::cleanCssIdentifier($settings['liclass']);
162 // Adding custom link classes.
163 if (strpos($settings['hlclass'], ' ') !== FALSE) {
164 $l = explode(' ', $settings['hlclass']);
166 $link_class[] = Html::cleanCssIdentifier($c);
170 $link_class[] = Html::cleanCssIdentifier($settings['hlclass']);
173 // Add a class to external links.
174 $link_class[] = isset($item['options']['external']) ? 'sf-external' : '';
176 // Inserting link description (the "title" attribute) into the text.
177 if ($settings['add_linkdescription'] && !empty($item['description'])) {
178 $link_text = '@text <span class="sf-description">@description</span>';
179 $link_text_replace = array('@text' => $item['text'], '@description' => $item['description']);
181 $link_text = '@text';
182 $link_text_replace = array('@text' => $item['text']);
185 // Hiding link descriptions (the "title" attribute).
186 if ($settings['hide_linkdescription']) {
187 $item['options']['attributes']['title'] = '';
191 if ($item['hasChildren'] && $item['subtree'] && $item['expanded']) {
193 // Multi-column sub-menus.
194 if ($settings['multicolumn']) {
195 $multicolumn_wrapper = ($item['depth'] == $settings['multicolumn_depth']) ? TRUE : FALSE;
196 $multicolumn_column = ($item['depth'] == $settings['multicolumn_depth'] + 1) ? TRUE : FALSE;
197 $multicolumn_content = ($item['depth'] >= $settings['multicolumn_depth'] && $item['depth'] <= $settings['multicolumn_levels']) ? TRUE : FALSE;
201 // Preparing the cloned parent links to be added to the sub-menus.
202 if ($settings['clone_parent'] && $item['subtree']) {
203 $cloned_parent = $menu_item;
204 $cloned_parent->subtree = [];
207 $cloned_parent = FALSE;
210 // Render the sub-menu.
212 '#theme' => 'superfish_menu_items',
213 '#menu_name' => $element['#menu_name'],
214 '#tree' => $item['subtree'],
215 '#settings' => $sfsettings,
216 '#cloned_parent' => $cloned_parent
219 if ($item['subtree']) {
220 // Adding some more classes.
221 $item_class[] = $multicolumn_column ? 'sf-multicolumn-column' : '';
222 $item_class[] = $link_class[] = 'menuparent';
227 $item_class[] = 'sf-no-children';
230 // Preparing <li> classes for the theme.
231 $item_class = implode(' ', superfish_array_remove_empty($item_class));
233 // Merging link classes.
234 if (isset($item['options']['attributes']['class'])) {
235 $link_class_current = $item['options']['attributes']['class'];
236 if (!is_array($link_class_current)) {
237 $link_class_current = array($link_class_current);
239 $link_class = array_merge($link_class_current, superfish_array_remove_empty($link_class));
241 $item['options']['attributes']['class'] = superfish_array_remove_empty($link_class);
243 // Dirty fix! to only add a "menuparent" class.
244 $item['options_menuparent'] = $item['options'];
245 $item['options_menuparent']['attributes']['class'][] = 'menuparent';
246 $link_element = array(
248 '#title' => SafeMarkup::format($link_text, $link_text_replace),
249 '#url' => $item['url'],
250 '#options' => $item['options']
252 $link_element_menuparent = array(
254 '#title' => SafeMarkup::format($link_text, $link_text_replace),
255 '#url' => $item['url'],
256 '#options' => $item['options_menuparent']
259 $variables['menu_items'][] = array(
260 'id' => Html::getUniqueId($element['#menu_name'] .'-'. $item['id']),
261 'item_class' => $item_class,
262 'link' => $link_element,
263 'link_menuparent' => $link_element_menuparent,
264 'children' => $children,
265 'multicolumn_wrapper' => $multicolumn_wrapper,
266 'multicolumn_content' => $multicolumn_content,
267 'multicolumn_column' => $multicolumn_column