Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / superfish / superfish.theme.inc
1 <?php
2
3 /**
4  * @file
5  * Preprocessors and theme functions of the Superfish module.
6  */
7
8 use Drupal\Core\Url;
9 use Drupal\Core\Menu\InaccessibleMenuLink;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\Component\Utility\Html;
12 use Drupal\Component\Utility\SafeMarkup;
13
14 /**
15  * Prepares variables for the Superfish menu template.
16  *
17  * Default template: superfish.html.twig.
18  *
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.
27  *
28  * @see superfish.html.twig
29  */
30 function template_preprocess_superfish(array &$variables) {
31   $element = $variables['element'];
32
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,
39   ];
40
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'];
46
47   $menu_classes[] = ($direction === LanguageInterface::DIRECTION_RTL) ? 'rtl' : '';
48   if (strpos($element['#settings']['ulclass'], ' ') !== FALSE) {
49     $l = explode(' ', $element['#settings']['ulclass']);
50     foreach ($l as $c) {
51       $menu_classes[] = Html::cleanCssIdentifier($c);
52     }
53   }
54   else {
55     $menu_classes[] = Html::cleanCssIdentifier($element['#settings']['ulclass']);
56   }
57   $menu_classes = implode(' ', sf_array_filter($menu_classes));
58
59   $variables['id'] = $element['#html_id'];
60   $variables['menu_classes'] = $menu_classes;
61   $variables['menu_items'] = $menu_items_rendered;
62
63 }
64
65 /**
66  * Prepares variables for Superfish menu items templates.
67  *
68  * Default template: superfish-menu-items.html.twig.
69  *
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.
78  *
79  * @see superfish-menu-items.html.twig
80  */
81 function template_preprocess_superfish_menu_items(array &$variables) {
82
83   $element = $variables['element'];
84
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'];
88
89   $variables['menu_items'] = [];
90
91   $menu = $element['#tree'];
92
93   // sfTouchscreen.
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']);
98   }
99
100   $active_trails = \Drupal::service('menu.active_trail')
101     ->getActiveTrailIds($element['#menu_name']);
102
103   foreach ($menu as $key => $menu_item) {
104
105     if (NULL !== $menu_item->link &&
106         !($menu_item->link instanceof InaccessibleMenuLink)) {
107
108       $item_class = $link_class = [];
109       $multicolumn_wrapper = $multicolumn_column = $multicolumn_content = FALSE;
110
111       // Menu link properties.
112       $link = $menu_item->link->getPluginDefinition();
113
114       $item = [
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 ||
136             $link_url == $host . $request_uri) {
137           $link_class[] = 'is-active';
138         }
139       }
140
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';
148       }
149
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'];
154       }
155       // Indicates a cloned parent, i.e. does not exist in the actual menu tree.
156       $item_class[] = $element['#cloned_parent'] ? 'sf-clone-parent' : '';
157
158       // Adding custom <li> classes.
159       if (strpos($settings['liclass'], ' ') !== FALSE) {
160         $l = explode(' ', $settings['liclass']);
161         foreach ($l as $c) {
162           $item_class[] = Html::cleanCssIdentifier($c);
163         }
164       }
165       else {
166         $item_class[] = Html::cleanCssIdentifier($settings['liclass']);
167       }
168
169       // Adding custom link classes.
170       if (strpos($settings['hlclass'], ' ') !== FALSE) {
171         $l = explode(' ', $settings['hlclass']);
172         foreach ($l as $c) {
173           $link_class[] = Html::cleanCssIdentifier($c);
174         }
175       }
176       else {
177         $link_class[] = Html::cleanCssIdentifier($settings['hlclass']);
178       }
179
180       // Add a class to external links.
181       $link_class[] = isset($item['options']['external']) ? 'sf-external' : '';
182
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'],
189         ];
190       }
191       else {
192         $link_text = '@text';
193         $link_text_replace = [
194           '@text' => $item['text'],
195         ];
196       }
197
198       // Hiding link descriptions (the "title" attribute).
199       if ($settings['hide_linkdescription']) {
200         $item['options']['attributes']['title'] = '';
201       }
202
203       // Sub-menu.
204       if ($item['hasChildren'] && $item['subtree'] && $item['expanded']) {
205
206         // Multi-column sub-menus.
207         if ($settings['multicolumn']) {
208           if ($item['depth'] == $settings['multicolumn_depth']) {
209             $multicolumn_wrapper = TRUE;
210           }
211           else {
212             $multicolumn_wrapper = FALSE;
213           }
214           if ($item['depth'] == $settings['multicolumn_depth'] + 1) {
215             $multicolumn_column = TRUE;
216           }
217           else {
218             $multicolumn_column = FALSE;
219           }
220           if ($item['depth'] >= $settings['multicolumn_depth'] &&
221               $item['depth'] <= $settings['multicolumn_levels']) {
222             $multicolumn_content = TRUE;
223           }
224           else {
225             $multicolumn_content = FALSE;
226           }
227         }
228
229         // sfTouchscreen.
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 = [];
234         }
235         else {
236           $cloned_parent = FALSE;
237         }
238
239         // Render the sub-menu.
240         $children = [
241           '#theme'         => 'superfish_menu_items',
242           '#menu_name'     => $element['#menu_name'],
243           '#tree'          => $item['subtree'],
244           '#settings'      => $sfsettings,
245           '#cloned_parent' => $cloned_parent,
246         ];
247
248         if ($item['subtree']) {
249           // Adding some more classes.
250           $item_class[] = $multicolumn_column ? 'sf-multicolumn-column' : '';
251           $item_class[] = $link_class[] = 'menuparent';
252         }
253       }
254       else {
255         $children = '';
256         $item_class[] = 'sf-no-children';
257       }
258
259       // Preparing <li> classes for the theme.
260       $item_class = implode(' ', sf_array_filter($item_class));
261
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];
267         }
268         $link_class = array_merge(
269           $link_class_current,
270           sf_array_filter($link_class)
271         );
272       }
273       $item['options']['attributes']['class'] = sf_array_filter($link_class);
274
275       // Dirty fix! to only add a "menuparent" class.
276       $item['options_menuparent'] = $item['options'];
277       $item['options_menuparent']['attributes']['class'][] = 'menuparent';
278       $link_element = [
279         '#type' => 'link',
280         '#title' => SafeMarkup::format($link_text, $link_text_replace),
281         '#url' => $item['url'],
282         '#options' => $item['options'],
283       ];
284       $link_element_menuparent = [
285         '#type' => 'link',
286         '#title' => SafeMarkup::format($link_text, $link_text_replace),
287         '#url' => $item['url'],
288         '#options' => $item['options_menuparent'],
289       ];
290
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,
301       ];
302     }
303   }
304 }