Version 1
[yaffs-website] / web / modules / contrib / superfish / superfish.theme.inc
1 <?php
2 /**
3  * @file
4  * Preprocessors and theme functions of the Superfish module.
5  */
6
7 use Drupal\Core\Url;
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;
15
16 /**
17  * Prepares variables for the Superfish menu template.
18  *
19  * Default template: superfish.html.twig.
20  *
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.
29  *
30  * @see superfish.html.twig
31  */
32
33 function template_preprocess_superfish(array &$variables) {
34   $element = $variables['element'];
35
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
42   );
43
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']);
51     foreach ($l as $c) {
52       $menu_classes[] = Html::cleanCssIdentifier($c);
53     }
54   }
55   else {
56     $menu_classes[] = Html::cleanCssIdentifier($element['#settings']['ulclass']);
57   }
58   $menu_classes = implode(' ', superfish_array_remove_empty($menu_classes));
59
60   $variables['id'] = $element['#html_id'];
61   $variables['menu_classes'] = $menu_classes;
62   $variables['menu_items'] = $menu_items_rendered;
63
64 }
65
66 /**
67  * Prepares variables for Superfish menu items templates.
68  *
69  * Default template: superfish-menu-items.html.twig.
70  *
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.
79  *
80  * @see superfish-menu-items.html.twig
81  */
82
83 function template_preprocess_superfish_menu_items(array &$variables) {
84
85   $element = $variables['element'];
86
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'];
90
91   $variables['menu_items'] = array();
92
93   $menu = $element['#tree'];
94
95   // sfTouchscreen.
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']);
100   }
101
102   $active_trails = \Drupal::service('menu.active_trail')->getActiveTrailIds($element['#menu_name']);
103
104   foreach ($menu as $key => $menu_item) {
105
106     if (null !== $menu_item->link && !($menu_item->link instanceof InaccessibleMenuLink)) {
107
108       $item_class = $link_class = array();
109       $multicolumn_wrapper = $multicolumn_column = $multicolumn_content = FALSE;
110
111       // Menu link properties.
112       $link = $menu_item->link->getPluginDefinition();
113
114       $item = array(
115         'id'            => $link['id'],
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
126       );
127
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';
137         }
138       }
139
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';
143       }
144
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' : '';
150
151       // Adding custom <li> classes.
152       if (strpos($settings['liclass'], ' ') !== FALSE) {
153         $l = explode(' ', $settings['liclass']);
154         foreach ($l as $c) {
155           $item_class[] = Html::cleanCssIdentifier($c);
156         }
157       }
158       else {
159         $item_class[] = Html::cleanCssIdentifier($settings['liclass']);
160       }
161
162       // Adding custom link classes.
163       if (strpos($settings['hlclass'], ' ') !== FALSE) {
164         $l = explode(' ', $settings['hlclass']);
165         foreach ($l as $c) {
166           $link_class[] = Html::cleanCssIdentifier($c);
167         }
168       }
169       else {
170         $link_class[] = Html::cleanCssIdentifier($settings['hlclass']);
171       }
172
173       // Add a class to external links.
174       $link_class[] = isset($item['options']['external']) ? 'sf-external' : '';
175
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']);
180       } else {
181         $link_text = '@text';
182         $link_text_replace = array('@text' => $item['text']);
183       }
184
185       // Hiding link descriptions (the "title" attribute).
186       if ($settings['hide_linkdescription']) {
187         $item['options']['attributes']['title'] = '';
188       }
189
190       // Sub-menu.
191       if ($item['hasChildren'] && $item['subtree'] && $item['expanded']) {
192
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;
198         }
199
200         // sfTouchscreen.
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 = [];
205         }
206         else {
207           $cloned_parent = FALSE;
208         }
209
210         // Render the sub-menu.
211         $children = array(
212           '#theme'         => 'superfish_menu_items',
213           '#menu_name'     => $element['#menu_name'],
214           '#tree'          => $item['subtree'],
215           '#settings'      => $sfsettings,
216           '#cloned_parent' => $cloned_parent
217         );
218
219         if ($item['subtree']) {
220           // Adding some more classes.
221           $item_class[] = $multicolumn_column ? 'sf-multicolumn-column' : '';
222           $item_class[] = $link_class[] = 'menuparent';
223         }
224       }
225       else {
226         $children = '';
227         $item_class[] = 'sf-no-children';
228       }
229
230       // Preparing <li> classes for the theme.
231       $item_class = implode(' ', superfish_array_remove_empty($item_class));
232
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);
238         }
239         $link_class = array_merge($link_class_current, superfish_array_remove_empty($link_class));
240       }
241       $item['options']['attributes']['class'] = superfish_array_remove_empty($link_class);
242
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(
247         '#type' => 'link',
248         '#title' => SafeMarkup::format($link_text, $link_text_replace),
249         '#url' => $item['url'],
250         '#options' => $item['options']
251       );
252       $link_element_menuparent = array(
253         '#type' => 'link',
254         '#title' => SafeMarkup::format($link_text, $link_text_replace),
255         '#url' => $item['url'],
256         '#options' => $item['options_menuparent']
257       );
258
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
268       );
269     }
270   }
271 }