Version 1
[yaffs-website] / web / modules / contrib / superfish / src / Plugin / Block / SuperfishBlock.php
1 <?php
2
3 namespace Drupal\superfish\Plugin\Block;
4
5 use Drupal\system\Plugin\Block\SystemMenuBlock;
6 use Drupal\Core\Menu\InaccessibleMenuLink;
7 use Drupal\Core\Menu\MenuTreeParameters;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Component\Utility\Html;
10
11 /**
12  * Provides a "Superfish" block.
13  *
14  * @Block(
15  *   id = "superfish",
16  *   admin_label = @Translation("Superfish"),
17  *   cache = -1,
18  *   category = @Translation("Superfish"),
19  *   deriver = "Drupal\system\Plugin\Derivative\SystemMenuBlock"
20  * )
21  */
22 class SuperfishBlock extends SystemMenuBlock {
23
24   /**
25    * Overrides \Drupal\block\BlockBase::blockForm().
26    */
27   public function blockForm($form, FormStateInterface $form_state) {
28     $form = parent::blockForm($form, $form_state);
29     $defaults = $this->defaultConfiguration();
30     $form['sf'] = array(
31       '#type' => 'details',
32       '#title' => $this->t('Block settings'),
33       '#open' => TRUE,
34     );
35     $form['sf']['superfish_type'] = array(
36       '#type' => 'radios',
37       '#title' => $this->t('Menu type'),
38       '#description' => '<em>(' . $this->t('Default') . ': ' . $this->t('Horizontal (single row)') . ')</em>',
39       '#default_value' => $this->configuration['menu_type'],
40       '#options' => array(
41         'horizontal' => $this->t('Horizontal (single row)'),
42         'navbar' => $this->t('Horizontal (double row)'),
43         'vertical' => $this->t('Vertical (stack)')
44       ),
45     );
46     $form['sf']['superfish_style'] = array(
47       '#type' => 'select',
48       '#title' => $this->t('Style'),
49       '#description' => '<em>(' . $this->t('Default') . ': ' . $this->t('None') . ')</em>',
50       '#default_value' => $this->configuration['style'],
51       '#options' => array(
52         'none' => $this->t('None'),
53         'default' => $this->t('Default'),
54         'black' => $this->t('Black'),
55         'blue' => $this->t('Blue'),
56         'coffee' => $this->t('Coffee'),
57         'white' => $this->t('White')
58       )
59     );
60     $form['sf']['superfish_arrow'] = array(
61       '#type' => 'checkbox',
62       '#title' => $this->t('Add arrows to parent menus'),
63       '#default_value' => $this->configuration['arrow'],
64     );
65     $form['sf']['superfish_shadow'] = array(
66       '#type' => 'checkbox',
67       '#title' => $this->t('Drop shadows'),
68       '#default_value' => $this->configuration['shadow'],
69     );
70     $form['sf']['superfish_slide'] = array(
71       '#type' => 'select',
72       '#title' => $this->t('Slide-in effect'),
73       '#description' => '<em>(' . $this->t('Default') . ': ' . $this->t('Vertical') . ')</em><br />' . ((count(superfish_effects()) == 4) ? $this->t('jQuery Easing plugin is not installed.') . '<br />' . $this->t('The plugin provides a handful number of animation effects, they can be used by uploading the \'jquery.easing.js\' file to the libraries directory within the \'easing\' directory (for example: sites/all/libraries/easing/jquery.easing.js). Refresh this page after the plugin is uploaded, this will make more effects available in the above list.') . '<br />' : ''),
74       '#default_value' => $this->configuration['slide'],
75       '#options' => superfish_effects(),
76     );
77     $form['sf-plugins'] = array(
78       '#type' => 'details',
79       '#title' => $this->t('Superfish plugins'),
80       '#open' => TRUE,
81     );
82     $form['sf-plugins']['superfish_supposition'] = array(
83       '#type' => 'checkbox',
84       '#title' => $this->t('jQuery Supposition'),
85       '#description' => $this->t('Relocates sub-menus when they would otherwise appear outside the browser window area.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('enabled') . ')</em>',
86       '#default_value' => $this->configuration['supposition'],
87     );
88     $form['sf-plugins']['superfish_hoverintent'] = array(
89       '#type' => 'checkbox',
90       '#title' => $this->t('jQuery hoverIntent'),
91       '#description' => $this->t('Prevents accidental firing of animations by waiting until the user\'s mouse slows down enough, hence determinig user\'s <em>intent</em>.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('enabled') . ')</em>',
92       '#default_value' => $this->configuration['hoverintent'],
93     );
94     $form['sf-plugins']['sf-touchscreen'] = array(
95       '#type' => 'details',
96       '#title' => $this->t('sf-Touchscreen'),
97       '#description' => $this->t('<strong>sf-Touchscreen</strong> provides touchscreen compatibility.') . ' <sup>(' . $this->t('The first click on a parent hyperlink shows its children and the second click opens the hyperlink.') . ')</sup>',
98       '#open' => FALSE,
99     );
100     $form['sf-plugins']['sf-touchscreen']['superfish_touch'] = array(
101       '#type' => 'radios',
102       '#default_value' => $this->configuration['touch'],
103       '#options' => array(
104         0 => $this->t('Disable') . '. <sup>(' . $this->t('Default') . ')</sup>',
105         1 => $this->t('Enable jQuery sf-Touchscreen plugin for this menu.'),
106         2 => $this->t('Enable jQuery sf-Touchscreen plugin for this menu depending on the user\'s Web browser <strong>window width</strong>.'),
107         3 => $this->t('Enable jQuery sf-Touchscreen plugin for this menu depending on the user\'s Web browser <strong>user agent</strong>.'),
108       ),
109     );
110     $form['sf-plugins']['sf-touchscreen']['superfish_touchbh'] = array(
111       '#type' => 'radios',
112       '#title' => 'Select a behaviour',
113       '#description' => $this->t('Using this plugin, the first click or tap will expand the sub-menu, here you can choose what a second click or tap should do.'),
114       '#default_value' => $this->configuration['touchbh'],
115       '#options' => array(
116         0 => $this->t('Opening the parent menu item link on the second tap.'),
117         1 => $this->t('Hiding the sub-menu on the second tap.'),
118         2 => $this->t('Hiding the sub-menu on the second tap, adding cloned parent links to the top of sub-menus as well.') . ' <sup>(' . $this->t('Default') . ')</sup>',
119       ),
120     );
121     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-windowwidth'] = array(
122       '#type' => 'details',
123       '#title' => $this->t('Window width settings'),
124       '#description' => $this->t('sf-Touchscreen will be enabled only if the width of user\'s Web browser window is smaller than the below value.') . '<br /><br />' . $this->t('Please note that in most cases such a meta tag is necessary for this feature to work properly:') . '<br /><code>&lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;</code>',
125       '#open' => TRUE,
126     );
127     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-windowwidth']['superfish_touchbp'] = array(
128       '#type' => 'textfield',
129       '#description' => $this->t('Also known as "Breakpoint".') . ' <em>(' . $this->t('Default') . ': 768)</em>',
130       '#default_value' => $this->configuration['touchbp'],
131       '#field_suffix' => $this->t('pixels'),
132       '#size' => 10,
133     );
134     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-useragent'] = array(
135       '#type' => 'details',
136       '#title' => $this->t('User agent settings'),
137       '#open' => TRUE,
138     );
139     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-useragent']['superfish_touchua'] = array(
140       '#type' => 'radios',
141       '#default_value' => $this->configuration['touchua'],
142       '#options' => array(
143         0 => $this->t('Use the pre-defined list of the <strong>user agents</strong>.') . '<sup>(' . $this->t('Default') . ') (' . $this->t('Recommended') . ')</sup>',
144         1 => $this->t('Use the custom list of the <strong>user agents</strong>.'),
145       ),
146     );
147     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-useragent']['superfish_touchual'] = array(
148       '#type' => 'textfield',
149       '#title' => $this->t('Custom list of the user agents'),
150       '#description' => $this->t('Could be partial or complete. (Asterisk separated)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Examples') . ':<ul><li>iPhone*Android*iPad <sup>(' . $this->t('Recommended') . ')</sup></li><li>Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.0 * Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405</li></ul>' . ((isset($_SERVER['HTTP_USER_AGENT'])) ? '<br />' . $this->t('<strong>UA string of the current Web browser:</strong>') . ' ' . $_SERVER['HTTP_USER_AGENT'] : ''),
151       '#default_value' => $this->configuration['touchual'],
152       '#size' => 100,
153       '#maxlength' => 2000,
154     );
155     $form['sf-plugins']['sf-touchscreen']['sf-touchscreen-useragent']['superfish_touchuam'] = array(
156       '#type' => 'select',
157       '#title' => $this->t('<strong>User agent</strong> detection method'),
158       '#description' => '<em>(' . $this->t('Default') . ': ' . $this->t('Client-side (JavaScript)') . ')</em>',
159       '#default_value' => $this->configuration['touchuam'],
160       '#options' => array(
161         0 => $this->t('Client-side (JavaScript)'),
162         1 => $this->t('Server-side (PHP)')
163       ),
164     );
165     $form['sf-plugins']['sf-smallscreen'] = array(
166       '#type' => 'details',
167       '#title' => $this->t('sf-Smallscreen'),
168       '#description' => $this->t('<strong>sf-Smallscreen</strong> provides small-screen compatibility for your menus.') . ' <sup>(' . $this->t('Converts the dropdown into a &lt;select&gt; element.') . ')</sup>',
169       '#open' => FALSE,
170     );
171     $form['sf-plugins']['sf-smallscreen']['superfish_small'] = array(
172       '#type' => 'radios',
173       '#default_value' => $this->configuration['small'],
174       '#options' => array(
175         0 => $this->t('Disable') . '.',
176         1 => $this->t('Enable jQuery sf-Smallscreen plugin for this menu.'),
177         2 => $this->t('Enable jQuery sf-Smallscreen plugin for this menu depending on the user\'s Web browser <strong>window width</strong>.') . ' <sup>(' . $this->t('Default') . ')</sup>',
178         3 => $this->t('Enable jQuery sf-Smallscreen plugin for this menu depending on the user\'s Web browser <strong>user agent</strong>.'),
179       ),
180     );
181     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-windowwidth'] = array(
182       '#type' => 'details',
183       '#title' => $this->t('Window width settings'),
184       '#description' => $this->t('sf-Smallscreen will be enabled only if the width of user\'s Web browser window is smaller than the below value.') . '<br /><br />' . $this->t('Please note that in most cases such a meta tag is necessary for this feature to work properly:') . '<br /><code>&lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;</code>',
185       '#open' => TRUE,
186     );
187     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-windowwidth']['superfish_smallbp'] = array(
188       '#type' => 'textfield',
189       '#description' => $this->t('Also known as "Breakpoint".') . ' <em>(' . $this->t('Default') . ': 768)</em>',
190       '#default_value' => $this->configuration['smallbp'],
191       '#field_suffix' => $this->t('pixels'),
192       '#size' => 10,
193     );
194     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-useragent'] = array(
195       '#type' => 'details',
196       '#title' => $this->t('User agent settings'),
197       '#open' => TRUE,
198     );
199     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-useragent']['superfish_smallua'] = array(
200       '#type' => 'radios',
201       '#default_value' => $this->configuration['smallua'],
202       '#options' => array(
203         0 => $this->t('Use the pre-defined list of the <strong>user agents</strong>.') . '<sup>(' . $this->t('Default') . ') (' . $this->t('Recommended') . ')</sup>',
204         1 => $this->t('Use the custom list of the <strong>user agents</strong>.'),
205       ),
206     );
207     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-useragent']['superfish_smallual'] = array(
208       '#type' => 'textfield',
209       '#title' => $this->t('Custom list of the user agents'),
210       '#description' => $this->t('Could be partial or complete. (Asterisk separated)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Examples') . ':<ul><li>iPhone*Android*iPad <sup>(' . $this->t('Recommended') . ')</sup></li><li>Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.0 * Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405</li></ul>' . ((isset($_SERVER['HTTP_USER_AGENT'])) ? '<br />' . $this->t('<strong>UA string of the current Web browser:</strong>') . ' ' . $_SERVER['HTTP_USER_AGENT'] : ''),
211       '#default_value' => $this->configuration['smallual'],
212       '#size' => 100,
213       '#maxlength' => 2000,
214     );
215     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-useragent']['superfish_smalluam'] = array(
216       '#type' => 'select',
217       '#title' => $this->t('<strong>User agent</strong> detection method'),
218       '#description' => '<em>(' . $this->t('Default') . ': ' . $this->t('Client-side (JavaScript)') . ')</em>',
219       '#default_value' => $this->configuration['smalluam'],
220       '#options' => array(
221         0 => $this->t('Client-side (JavaScript)'),
222         1 => $this->t('Server-side (PHP)')
223       ),
224     );
225     $form['sf-plugins']['sf-smallscreen']['superfish_smallact'] = array(
226       '#type' => 'radios',
227       '#title' => $this->t('Select a type'),
228       '#default_value' => $this->configuration['smallact'],
229       '#options' => array(
230         0 => $this->t('Convert the menu to a &lt;select&gt; element.'),
231         1 => $this->t('Convert the menu to an accordion menu.') . ' <sup>(' . $this->t('Default') . ')</sup>',
232       ),
233     );
234     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select'] = array(
235       '#type' => 'details',
236       '#title' => $this->t('&lt;select&gt; settings'),
237       '#open' => TRUE,
238     );
239     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['superfish_smallset'] = array(
240       '#type' => 'textfield',
241       '#title' => $this->t('&lt;select&gt; title'),
242       '#description' => $this->t('By default the first item in the &lt;select&gt; element will be the name of the parent menu or the title of this block, you can change this by setting a custom title.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Example') . ': <em> - ' . $this->t('Main Menu') . ' - </em>.',
243       '#default_value' => $this->configuration['smallset'],
244       '#size' => 50,
245       '#maxlength' => 500,
246     );
247     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['superfish_smallasa'] = array(
248       '#type' => 'checkbox',
249       '#title' => $this->t('Add <em>selected</em> attribute to the &lt;option&gt; element with the class <strong>active</strong> .'),
250       '#description' => $this->t('Makes pre-selected the item linked to the active page when the page loads.'),
251       '#default_value' => $this->configuration['smallasa'],
252     );
253     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more'] = array(
254       '#type' => 'details',
255       '#title' => $this->t('More'),
256       '#open' => FALSE,
257     );
258     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallcmc'] = array(
259       '#type' => 'checkbox',
260       '#title' => $this->t('Copy the main &lt;ul&gt; classes to the &lt;select&gt;.') . ' <sup><em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em></sup>',
261       '#default_value' => $this->configuration['smallcmc'],
262     );
263     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallecm'] = array(
264       '#type' => 'textfield',
265       '#title' => $this->t('Exclude these classes from the &lt;select&gt; element'),
266       '#description' => $this->t('Comma separated') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em>',
267       '#default_value' => $this->configuration['smallecm'],
268       '#size' => 100,
269       '#maxlength' => 1000,
270       '#states' => array(
271         'enabled' => array(
272          ':input[name="superfish_smallcmc' . '"]' => array('checked' => TRUE),
273         ),
274       ),
275     );
276     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallchc'] = array(
277       '#type' => 'checkbox',
278       '#title' => $this->t('Copy the hyperlink classes to the &lt;option&gt; elements of the &lt;select&gt;.') . ' <sup><em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em></sup>',
279       '#default_value' => $this->configuration['smallchc'],
280     );
281     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallech'] = array(
282       '#type' => 'textfield',
283       '#title' => $this->t('Exclude these classes from the &lt;option&gt; elements of the &lt;select&gt;'),
284       '#description' => $this->t('Comma separated') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em>',
285       '#default_value' => $this->configuration['smallech'],
286       '#size' => 100,
287       '#maxlength' => 1000,
288       '#states' => array(
289         'enabled' => array(
290          ':input[name="superfish_smallchc' . '"]' => array('checked' => TRUE),
291         ),
292       ),
293     );
294     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallicm'] = array(
295       '#type' => 'textfield',
296       '#title' => $this->t('Include these classes in the &lt;select&gt; element'),
297       '#description' => $this->t('Comma separated') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em>',
298       '#default_value' => $this->configuration['smallicm'],
299       '#size' => 100,
300       '#maxlength' => 1000,
301     );
302     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-select']['sf-smallscreen-select-more']['superfish_smallich'] = array(
303       '#type' => 'textfield',
304       '#title' => $this->t('Include these classes in the &lt;option&gt; elements of the &lt;select&gt;'),
305       '#description' => $this->t('Comma separated') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em>',
306       '#default_value' => $this->configuration['smallich'],
307       '#size' => 100,
308       '#maxlength' => 1000,
309     );
310   $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-accordion'] = array(
311       '#type' => 'details',
312       '#title' => $this->t('Accordion settings'),
313       '#open' => TRUE,
314     );
315     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-accordion']['superfish_smallamt'] = array(
316       '#type' => 'textfield',
317       '#title' => $this->t('Accordion menu title'),
318       '#description' => $this->t('By default the caption of the accordion toggle switch will be the name of the parent menu or the title of this block, you can change this by setting a custom title.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Example') . ': <em>' . $this->t('Menu') . '</em>.',
319       '#default_value' => $this->configuration['smallamt'],
320       '#size' => 50,
321       '#maxlength' => 500,
322     );
323     $form['sf-plugins']['sf-smallscreen']['sf-smallscreen-accordion']['superfish_smallabt'] = array(
324       '#type' => 'radios',
325       '#title' => $this->t('Accordion button type'),
326       '#default_value' => $this->configuration['smallabt'],
327       '#options' => array(
328         0 => $this->t('Use parent menu items as buttons.'),
329         1 => $this->t('Use parent menu items as buttons, add cloned parent links to sub-menus as well.') . ' <sup>(' . $this->t('Default') . ')</sup>',
330         2 => $this->t('Create new links next to parent menu item links and use them as buttons.'),
331       ),
332     );
333     $form['sf-plugins']['sf-supersubs'] = array(
334       '#type' => 'details',
335       '#title' => $this->t('Supersubs'),
336       '#description' => $this->t('<strong>Supersubs</strong> makes it possible to define custom widths for your menus.'),
337       '#open' => FALSE,
338     );
339     $form['sf-plugins']['sf-supersubs']['superfish_supersubs'] = array(
340       '#type' => 'checkbox',
341       '#title' => $this->t('Enable Supersubs for this menu.'),
342       '#default_value' => $this->configuration['supersubs'],
343     );
344     $form['sf-plugins']['sf-supersubs']['superfish_minwidth'] = array(
345       '#type' => 'textfield',
346       '#title' => $this->t('Minimum width'),
347       '#description' => $this->t('Minimum width for sub-menus, in <strong>em</strong> units.') . ' <em>(' . $this->t('Default') . ': 12)</em>',
348       '#default_value' => $this->configuration['minwidth'],
349       '#size' => 10,
350     );
351     $form['sf-plugins']['sf-supersubs']['superfish_maxwidth'] = array(
352       '#type' => 'textfield',
353       '#title' => $this->t('Maximum width'),
354       '#description' => $this->t('Maximum width for sub-menus, in <strong>em</strong> units.') . ' <em>(' . $this->t('Default') . ': 27)</em>',
355       '#default_value' => $this->configuration['maxwidth'],
356       '#size' => 10,
357     );
358     $form['sf-multicolumn'] = array(
359       '#type' => 'details',
360       '#title' => $this->t('Multi-column sub-menus'),
361       '#open' => FALSE,
362     );
363     $form['sf-multicolumn']['superfish_multicolumn'] = array(
364       '#type' => 'checkbox',
365       '#title' => $this->t('Enable multi-column sub-menus.'),
366       '#default_value' => $this->configuration['multicolumn'],
367     );
368     $form['sf-multicolumn']['superfish_multicolumn_depth'] = array(
369       '#type' => 'select',
370       '#title' => $this->t('Start from depth'),
371       '#description' => $this->t('The depth of the first instance of multi-column sub-menus.') . ' <em>(' . $this->t('Default') . ': 1)</em>',
372       '#default_value' => $this->configuration['multicolumn_depth'],
373       '#options' => array_combine(range(1, 10),range(1, 10)),
374     );
375     $form['sf-multicolumn']['superfish_multicolumn_levels'] = array(
376       '#type' => 'select',
377       '#title' => $this->t('Levels'),
378       '#description' => $this->t('The amount of sub-menu levels that will be included in the multi-column sub-menu.') . ' <em>(' . $this->t('Default') . ': 1)</em>',
379       '#default_value' => $this->configuration['multicolumn_levels'],
380       '#options' => array_combine(range(1, 10),range(1, 10)),
381     );
382     $form['sf-advanced'] = array(
383       '#type' => 'details',
384       '#title' => $this->t('Advanced settings'),
385       '#open' => FALSE,
386     );
387     $form['sf-advanced']['sf-settings'] = array(
388       '#type' => 'details',
389       '#title' => $this->t('Superfish'),
390       '#open' => FALSE,
391     );
392     $form['sf-advanced']['sf-settings']['superfish_speed'] = array(
393       '#type' => 'textfield',
394       '#title' => $this->t('Animation speed'),
395       '#description' => $this->t('The speed of the animation either in <strong>milliseconds</strong> or pre-defined values (<strong>slow, normal, fast</strong>).') . ' <em>(' . $this->t('Default') . ': fast)</em>',
396       '#default_value' => $this->configuration['speed'],
397       '#size' => 15,
398     );
399     $form['sf-advanced']['sf-settings']['superfish_delay'] = array(
400       '#type' => 'number',
401       '#title' => $this->t('Mouse delay'),
402       '#description' => $this->t('The delay in <strong>milliseconds</strong> that the mouse can remain outside a sub-menu without it closing.') . ' <em>(' . $this->t('Default') . ': 800)</em>',
403       '#default_value' => $this->configuration['delay'],
404       '#size' => 15,
405     );
406     $form['sf-advanced']['sf-settings']['superfish_pathlevels'] = array(
407       '#type' => 'select',
408       '#title' => $this->t('Path levels'),
409       '#description' => $this->t('The amount of sub-menu levels that remain open or are restored using the ".active-trail" class.') . ' <em>(' . $this->t('Default') . ': 1)</em><br />' . $this->t('Change this setting <strong>only and only</strong> if you are <strong>totally sure</strong> of what you are doing.'),
410       '#default_value' => $this->configuration['pathlevels'],
411       '#options' => array_combine(range(0, 10),range(0, 10)),
412     );
413     $form['sf-advanced']['sf-hyperlinks'] = array(
414       '#type' => 'details',
415       '#title' => $this->t('Hyperlinks'),
416       '#open' => TRUE,
417     );
418     $form['sf-advanced']['sf-hyperlinks']['superfish_expanded'] = array(
419       '#type' => 'checkbox',
420       '#title' => $this->t('Take "Expanded" option into effect.'),
421       '#description' => $this->t('By enabling this option, only parent menu items with <em>Expanded</em> option enabled will have their submenus appear.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em>',
422       '#default_value' => $this->configuration['expanded'],
423     );
424     $form['sf-advanced']['sf-hyperlinks']['superfish_clone_parent'] = array(
425       '#type' => 'checkbox',
426       '#title' => $this->t('Add cloned parent links to the top of sub-menus.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em>',
427       '#default_value' => $this->configuration['clone_parent'],
428     );
429     $form['sf-advanced']['sf-hyperlinks']['superfish_hide_linkdescription'] = array(
430       '#type' => 'checkbox',
431       '#title' => $this->t('Disable hyperlink descriptions ("title" attribute)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em>',
432       '#default_value' => $this->configuration['hide_linkdescription'],
433     );
434     $form['sf-advanced']['sf-hyperlinks']['superfish_add_linkdescription'] = array(
435       '#type' => 'checkbox',
436       '#title' => $this->t('Insert hyperlink descriptions ("title" attribute) into hyperlink texts.') . ' <em>(' . $this->t('Default') . ': ' . $this->t('disabled') . ')</em>',
437       '#default_value' => $this->configuration['add_linkdescription'],
438     );
439     $form['sf-advanced']['sf-hyperlinks']['superfish_itemdepth'] = array(
440       '#type' => 'checkbox',
441       '#title' => $this->t('Add <strong>item depth</strong> class to menu items and their hyperlinks.') . '<em>(sf-depth-1, sf-depth-2, sf-depth-3, ...)</em> <em>(' . $this->t('Default') . ': ' . $this->t('enabled') . ')</em>',
442       '#default_value' => $this->configuration['link_depth_class'],
443     );
444     $form['sf-advanced']['sf-custom-classes'] = array(
445       '#type' => 'details',
446       '#title' => $this->t('Custom classes'),
447       '#open' => TRUE,
448     );
449     $form['sf-advanced']['sf-custom-classes']['superfish_ulclass'] = array(
450       '#type' => 'textfield',
451       '#title' => $this->t('For the main UL'),
452       '#description' => $this->t('(Space separated, without dots)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Example') . ': top-menu category-science',
453       '#default_value' => $this->configuration['custom_list_class'],
454       '#size' => 50,
455       '#maxlength' => 1000,
456     );
457     $form['sf-advanced']['sf-custom-classes']['superfish_liclass'] = array(
458       '#type' => 'textfield',
459       '#title' => $this->t('For the list items'),
460       '#description' => $this->t('(Space separated, without dots)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Example') . ': science-sub',
461       '#default_value' => $this->configuration['custom_item_class'],
462       '#size' => 50,
463       '#maxlength' => 1000,
464     );
465     $form['sf-advanced']['sf-custom-classes']['superfish_hlclass'] = array(
466       '#type' => 'textfield',
467       '#title' => $this->t('For the hyperlinks'),
468       '#description' => $this->t('(Space separated, without dots)') . ' <em>(' . $this->t('Default') . ': ' . $this->t('empty') . ')</em><br />' . $this->t('Example') . ': science-link',
469       '#default_value' => $this->configuration['custom_link_class'],
470       '#size' => 50,
471       '#maxlength' => 1000,
472     );
473     return $form;
474   }
475
476   /**
477    * Overrides \Drupal\block\BlockBase::blockValiate().
478    */
479   public function blockValidate($form, FormStateInterface $form_state) {
480     /**
481      // Commented out for now as I couldn't get validation to work, with RC4 at least.
482
483     $touch = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touch'));
484     $touchbp = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-windowwidth', 'superfish_touchbp'));
485     $touchua = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touchua'));
486     $touchual = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touchual'));
487     $small = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_small'));
488     $smallbp = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smallbp'));
489     $smallua = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smallua'));
490     $smallual = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smallual'));
491     $minwidth = $form_state->getValue(array('sf-plugins', 'sf-supersubs', 'superfish_minwidth'));
492     $maxwidth = $form_state->getValue(array('sf-plugins', 'sf-supersubs', 'superfish_maxwidth'));
493     $speed = $form_state->getValue(array('sf-advanced', 'sf-settings', 'superfish_speed'));
494     $delay = $form_state->getValue(array('sf-advanced', 'sf-settings', 'superfish_delay'));
495
496     if (!is_numeric($speed) && !in_array($speed, array('slow', 'normal', 'fast'))) {
497       $form_state->setErrorByName('superfish_speed', t('Unacceptable value entered for the "Animation speed" option.'));
498     }
499     if (!is_numeric($delay)) {
500       $form_state->setErrorByName('superfish_delay', t('Unacceptable value entered for the "Mouse delay" option.'));
501     }
502     if ($touch == 2 && $touchbp == '') {
503       $form_state->setErrorByName('superfish_touchbp', t('"sfTouchscreen Breakpoint" option cannot be empty.'));
504     }
505     if (!is_numeric($touchbp)) {
506       $form_state->setErrorByName('superfish_touchbp', t('Unacceptable value enterd for the "sfTouchscreen Breakpoint" option.'));
507     }
508     if ($touch == 3 && $touchua == 1 && $touchual == '') {
509       $form_state->setErrorByName('superfish_touchual', t('List of the touch-screen user agents cannot be empty.'));
510     }
511     if ($small == 2 && $smallbp == '') {
512       $form_state->setErrorByName('superfish_smallbp', t('"sfSmallscreen Breakpoint" option cannot be empty.'));
513     }
514     if (!is_numeric($smallbp)) {
515       $form_state->setErrorByName('superfish_smallbp', t('Unacceptable value entered for the "sfSmallscreen Breakpoint" option.'));
516     }
517     if ($small == 3 && $smallua == 1 && $smallual == '') {
518       $form_state->setErrorByName('superfish_smallual', t('List of the small-screen user agents cannot be empty.'));
519     }
520
521     $supersubs_error = FALSE;
522     if (!is_numeric($minwidth)) {
523       $form_state->setErrorByName('superfish_minwidth', t('Unacceptable value entered for the "Supersubs minimum width" option.'));
524       $supersubs_error = TRUE;
525     }
526     if (!is_numeric($maxwidth)) {
527       $form_state->setErrorByName('superfish_maxwidth', t('Unacceptable value entered for the "Supersubs maximum width" option.'));
528       $supersubs_error = TRUE;
529     }
530     if ($supersubs_error !== TRUE && $minwidth > $maxwidth) {
531       $form_state->setErrorByName('superfish_maxwidth', t('Supersubs "maximum width" has to be bigger than the "minimum width".'));
532     }
533
534     parent::blockValidate($form, $form_state);
535     */
536   }
537
538   /**
539    * Overrides \Drupal\block\BlockBase::blockSubmit().
540    */
541   public function blockSubmit($form, FormStateInterface $form_state) {
542
543     $this->configuration['level'] = $form_state->getValue('level');
544     $this->configuration['depth'] = $form_state->getValue('depth');
545     $this->configuration['menu_type'] = $form_state->getValue(array('sf', 'superfish_type'));
546     $this->configuration['style'] = $form_state->getValue(array('sf', 'superfish_style'));
547     $this->configuration['arrow'] = $form_state->getValue(array('sf', 'superfish_arrow'));
548     $this->configuration['shadow'] = $form_state->getValue(array('sf', 'superfish_shadow'));
549     $this->configuration['slide'] = $form_state->getValue(array('sf', 'superfish_slide'));
550
551     $this->configuration['supposition'] = $form_state->getValue(array('sf-plugins', 'superfish_supposition'));
552     $this->configuration['hoverintent'] = $form_state->getValue(array('sf-plugins', 'superfish_hoverintent'));
553
554     $this->configuration['touch'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'superfish_touch'));
555     $this->configuration['touchbh'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'superfish_touchbh'));
556     $this->configuration['touchbp'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-windowwidth', 'superfish_touchbp'));
557     $this->configuration['touchua'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touchua'));
558     $this->configuration['touchual'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touchual'));
559     $this->configuration['touchuam'] = $form_state->getValue(array('sf-plugins', 'sf-touchscreen', 'sf-touchscreen-useragent', 'superfish_touchuam'));
560
561     $this->configuration['small'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'superfish_small'));
562     $this->configuration['smallact'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'superfish_smallact'));
563     $this->configuration['smallbp'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-windowwidth', 'superfish_smallbp'));
564     $this->configuration['smallua'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smallua'));
565     $this->configuration['smallual'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smallual'));
566     $this->configuration['smalluam'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-useragent', 'superfish_smalluam'));
567     $this->configuration['smallset'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'superfish_smallset'));
568     $this->configuration['smallasa'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'superfish_smallasa'));
569     $this->configuration['smallcmc'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallcmc'));
570     $this->configuration['smallecm'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallecm'));
571     $this->configuration['smallchc'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallchc'));
572     $this->configuration['smallech'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallech'));
573     $this->configuration['smallicm'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallicm'));
574     $this->configuration['smallich'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-select', 'sf-smallscreen-select-more', 'superfish_smallich'));
575     $this->configuration['smallamt'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-accordion', 'superfish_smallamt'));
576     $this->configuration['smallabt'] = $form_state->getValue(array('sf-plugins', 'sf-smallscreen', 'sf-smallscreen-accordion', 'superfish_smallabt'));
577
578     $this->configuration['supersubs'] = $form_state->getValue(array('sf-plugins', 'sf-supersubs', 'superfish_supersubs'));
579     $this->configuration['minwidth'] = $form_state->getValue(array('sf-plugins', 'sf-supersubs', 'superfish_minwidth'));
580     $this->configuration['maxwidth'] = $form_state->getValue(array('sf-plugins', 'sf-supersubs', 'superfish_maxwidth'));
581
582     $this->configuration['multicolumn'] = $form_state->getValue(array('sf-multicolumn', 'superfish_multicolumn'));
583     $this->configuration['multicolumn_depth'] = $form_state->getValue(array('sf-multicolumn', 'superfish_multicolumn_depth'));
584     $this->configuration['multicolumn_levels'] = $form_state->getValue(array('sf-multicolumn', 'superfish_multicolumn_levels'));
585
586     $this->configuration['speed'] = $form_state->getValue(array('sf-advanced', 'sf-settings', 'superfish_speed'));
587     $this->configuration['delay'] = $form_state->getValue(array('sf-advanced', 'sf-settings', 'superfish_delay'));
588     $this->configuration['pathlevels'] = $form_state->getValue(array('sf-advanced', 'sf-settings', 'superfish_pathlevels'));
589     $this->configuration['expanded'] = $form_state->getValue(array('sf-advanced', 'sf-hyperlinks', 'superfish_expanded'));
590     $this->configuration['clone_parent'] = $form_state->getValue(array('sf-advanced', 'sf-hyperlinks', 'superfish_clone_parent'));
591     $this->configuration['hide_linkdescription'] = $form_state->getValue(array('sf-advanced', 'sf-hyperlinks', 'superfish_hide_linkdescription'));
592     $this->configuration['add_linkdescription'] = $form_state->getValue(array('sf-advanced', 'sf-hyperlinks', 'superfish_add_linkdescription'));
593     $this->configuration['link_depth_class'] = $form_state->getValue(array('sf-advanced', 'sf-hyperlinks', 'superfish_itemdepth'));
594     $this->configuration['custom_list_class'] = $form_state->getValue(array('sf-advanced', 'sf-custom-classes', 'superfish_ulclass'));
595     $this->configuration['custom_item_class'] = $form_state->getValue(array('sf-advanced', 'sf-custom-classes', 'superfish_liclass'));
596     $this->configuration['custom_link_class'] = $form_state->getValue(array('sf-advanced', 'sf-custom-classes', 'superfish_hlclass'));
597   }
598
599   /**
600    * Implements \Drupal\block\BlockBase::build().
601    */
602   public function build() {
603
604   $build = array();
605
606     // Block settings which will be passed to the Superfish themes.
607     $sfsettings = array();
608     $sfsettings['level']               = $this->configuration['level'];
609     $sfsettings['depth']                = $this->configuration['depth'];
610     $sfsettings['menu_type']            = $this->configuration['menu_type'];
611     $sfsettings['style']                = $this->configuration['style'];
612     $sfsettings['expanded']             = $this->configuration['expanded'];
613     $sfsettings['itemdepth']            = $this->configuration['link_depth_class'];
614     $sfsettings['ulclass']              = $this->configuration['custom_list_class'];
615     $sfsettings['liclass']              = $this->configuration['custom_item_class'];
616     $sfsettings['hlclass']              = $this->configuration['custom_link_class'];
617     $sfsettings['clone_parent']         = $this->configuration['clone_parent'];
618     $sfsettings['hide_linkdescription'] = $this->configuration['hide_linkdescription'];
619     $sfsettings['add_linkdescription']  = $this->configuration['add_linkdescription'];
620     $sfsettings['multicolumn']          = $this->configuration['multicolumn'];
621     $sfsettings['multicolumn_depth']    = ($this->configuration['menu_type'] == 'navbar' && $this->configuration['multicolumn_depth'] == 1) ? 2 : $this->configuration['multicolumn_depth'];
622     $sfsettings['multicolumn_levels']   = $this->configuration['multicolumn_levels'] + $sfsettings['multicolumn_depth'];
623
624     // jQuery plugin options which will be passed to the Drupal behavior.
625     $sfoptions = array();
626     $sfoptions['pathClass'] = ($sfsettings['menu_type'] == 'navbar') ? 'active-trail' : '';
627     $sfoptions['pathLevels'] = ($this->configuration['pathlevels'] != 1) ? $this->configuration['pathlevels'] : '';
628     $sfoptions['delay'] = ($this->configuration['delay'] != 800) ? $this->configuration['delay'] : '';
629     $sfoptions['animation']['opacity'] = 'show';
630     $slide = $this->configuration['slide'];
631     if (strpos($slide, '_')) {
632       $slide = explode('_', $slide);
633       switch ($slide[1]) {
634         case 'vertical':
635           $sfoptions['animation']['height'] = array('show', $slide[0]);
636         break;
637         case 'horizontal':
638           $sfoptions['animation']['width'] = array('show', $slide[0]);
639         break;
640         case 'diagonal':
641           $sfoptions['animation']['height'] = array('show', $slide[0]);
642           $sfoptions['animation']['width'] = array('show', $slide[0]);
643         break;
644       }
645       $build['#attached']['library'][] = 'superfish/superfish_easing';
646     }
647     else {
648       switch ($slide) {
649         case 'vertical':
650           $sfoptions['animation']['height'] = 'show';
651         break;
652         case 'horizontal':
653           $sfoptions['animation']['width'] = 'show';
654         break;
655         case 'diagonal':
656           $sfoptions['animation']['height'] = 'show';
657           $sfoptions['animation']['width'] = 'show';
658         break;
659       }
660     }
661     $speed = $this->configuration['speed'];
662     if ($speed != 'normal') {
663       $sfoptions['speed'] = ((is_numeric($speed)) ? (int)$speed : (($speed == ('slow' || 'normal' || 'fast')) ? $speed : ''));
664     }
665     $sfoptions['autoArrows'] = ($this->configuration['arrow'] == 0) ? FALSE : '';
666     $sfoptions['dropShadows'] = ($this->configuration['shadow'] == 0) ? FALSE : '';
667
668     if ($this->configuration['hoverintent']) {
669       $build['#attached']['library'][] = 'superfish/superfish_hoverintent';
670     }
671     else {
672       $sfoptions['disableHI'] = TRUE;
673     }
674     $sfoptions = superfish_array_remove_empty($sfoptions);
675
676     // Options for Superfish sub-plugins.
677     $sfplugins = array();
678     $touchscreen = $this->configuration['touch'];
679     if ($touchscreen) {
680       $build['#attached']['library'][] = 'superfish/superfish_touchscreen';
681       $behaviour = $this->configuration['touchbh'];
682       $sfplugins['touchscreen']['behaviour'] = ($behaviour != 2) ? $behaviour : '';
683       switch ($touchscreen) {
684         case 1 :
685           $sfplugins['touchscreen']['mode'] = 'always_active';
686         break;
687         case 2 :
688           $sfplugins['touchscreen']['mode'] = 'window_width';
689           $tsbp = $this->configuration['touchbp'];
690           $sfplugins['touchscreen']['breakpoint'] = ($tsbp != 768) ? (int)$tsbp : '';
691         break;
692         case 3 :
693           // Which method to use for UA detection.
694           $tsuam = $this->configuration['touchuam'];
695           $tsua = $this->configuration['touchua'];
696           switch ($tsuam) {
697             // Client-side.
698             case 0 :
699               switch ($tsua) {
700                 case 0 :
701                   $sfplugins['touchscreen']['mode'] = 'useragent_predefined';
702                 break;
703                 case 1 :
704                   $sfplugins['touchscreen']['mode'] = 'useragent_custom';
705                   $tsual = drupal_strtolower($this->configuration['touchual']);
706                   if (strpos($tsual, '*')) {
707                     $tsual = str_replace('*', '|', $tsual);
708                   }
709                   $sfplugins['touchscreen']['useragent'] = $tsual;
710                 break;
711               }
712             break;
713             // Server-side.
714             case 1 :
715               if (isset($_SERVER['HTTP_USER_AGENT'])) {
716                 $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
717                 switch ($tsua) {
718                   // Use the pre-defined list of mobile UA strings.
719                   case 0 :
720                     if (preg_match('/(android|bb\d+|meego)|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
721                       $sfplugins['touchscreen']['mode'] = 'always_active';
722                       if ($behaviour == 2) {
723                         $sfsettings['clone_parent'] = 1;
724                       }
725                     }
726                   break;
727                   // Use the custom list of UA strings.
728                   case 1 :
729                     $tsual = drupal_strtolower($this->configuration['touchual']);
730                     $tsuac = array();
731                     if (strpos($tsual, '*')) {
732                       $tsual = explode('*', $tsual);
733                       foreach ($tsual as $ua) {
734                         $tsuac[] = (strpos($hua, $ua)) ? 1 : 0;
735                       }
736                     }
737                     else {
738                       $tsuac[] = (strpos($hua, $tsual)) ? 1 : 0;
739                     }
740                     if (in_array(1, $tsuac)) {
741                       $sfplugins['touchscreen']['mode'] = 'always_active';
742                       if ($behaviour == 2) {
743                         $sfsettings['clone_parent'] = 1;
744                       }
745                     }
746                   break;
747                 }
748               }
749             break;
750           }
751         break;
752       }
753     }
754
755     $smallscreen = $this->configuration['small'];
756     if ($smallscreen) {
757       $build['#attached']['library'][] = 'superfish/superfish_smallscreen';
758       switch ($smallscreen) {
759         case 1 :
760           $sfplugins['smallscreen']['mode'] = 'always_active';
761         break;
762         case 2 :
763           $sfplugins['smallscreen']['mode'] = 'window_width';
764           $ssbp = $this->configuration['smallbp'];
765           $sfplugins['smallscreen']['breakpoint'] = ($ssbp != 768) ? (int)$ssbp : '';
766         break;
767         case 3 :
768           // Which method to use for UA detection.
769           $ssuam = $this->configuration['smalluam'];
770           $ssua = $this->configuration['smallua'];
771           switch ($ssuam) {
772             // Client-side.
773             case 0 :
774               switch ($ssua) {
775                 case 0 :
776                   $sfplugins['smallscreen']['mode'] = 'useragent_predefined';
777                 break;
778                 case 1 :
779                   $sfplugins['smallscreen']['mode'] = 'useragent_custom';
780                   $ssual = drupal_strtolower($this->configuration['smallual']);
781                   if (strpos($ssual, '*')) {
782                     $ssual = str_replace('*', '|', $ssual);
783                   }
784                   $sfplugins['smallscreen']['useragent'] = $ssual;
785                 break;
786               }
787
788             break;
789             // Server-side.
790             case 1 :
791               if (isset($_SERVER['HTTP_USER_AGENT'])) {
792                 $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
793                 switch ($ssua) {
794                   // Use the pre-defined list of mobile UA strings.
795                   case 0 :
796                     if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
797                       $sfplugins['smallscreen']['mode'] = 'always_active';
798                     }
799                   break;
800                   // Use the custom list of UA strings.
801                   case 1 :
802                     $ssual = drupal_strtolower($this->configuration['smallual']);
803                     $ssuac = array();
804                     if (strpos($ssual, '*')) {
805                       $ssual = explode('*', $ssual);
806                       foreach ($ssual as $ua) {
807                         $ssuac[] = (strpos($hua, $ua)) ? 1 : 0;
808                       }
809                     }
810                     else {
811                       $ssuac[] = (strpos($hua, $ssual)) ? 1 : 0;
812                     }
813                     if (in_array(1, $ssuac)) {
814                       $sfplugins['smallscreen']['mode'] = 'always_active';
815                     }
816                   break;
817                 }
818               }
819             break;
820           }
821         break;
822       }
823       $type = $this->configuration['smallact'];
824       switch ($type) {
825         case 0:
826           $asa = $this->configuration['smallasa'];
827           $cmc = $this->configuration['smallcmc'];
828           $chc = $this->configuration['smallchc'];
829           $ecm = $this->configuration['smallecm'];
830           $ech = $this->configuration['smallech'];
831           $icm = $this->configuration['smallicm'];
832           $ich = $this->configuration['smallich'];
833
834           $sfplugins['smallscreen']['type'] = 'select';
835           $sfplugins['smallscreen']['addSelected'] = ($asa == 1) ? TRUE : '';
836           $sfplugins['smallscreen']['menuClasses'] = ($cmc == 1) ? TRUE : '';
837           $sfplugins['smallscreen']['hyperlinkClasses'] = ($chc == 1) ? TRUE : '';
838           $sfplugins['smallscreen']['excludeClass_menu'] = ($cmc == 1 && !empty($ecm)) ? $ecm : '';
839           $sfplugins['smallscreen']['excludeClass_hyperlink'] = ($chc == 1 && !empty($ech)) ? $ech : '';
840           $sfplugins['smallscreen']['includeClass_menu'] = (!empty($icm)) ? $icm : '';
841           $sfplugins['smallscreen']['includeClass_hyperlink'] = (!empty($ich)) ? $ich : '';
842         break;
843         case 1:
844           $ab = $this->configuration['smallabt'];
845           $sfplugins['smallscreen']['accordionButton'] = ($ab != 1) ? $ab : '';
846           $sfplugins['smallscreen']['expandText'] = ($this->t('Expand') != 'Expand') ? $this->t('Expand') : '';
847           $sfplugins['smallscreen']['collapseText'] = ($this->t('Collapse') != 'Collapse') ? $this->t('Collapse') : '';
848         break;
849       }
850     }
851
852     if ($this->configuration['supposition']) {
853       $sfplugins['supposition'] = TRUE;
854       $build['#attached']['library'][] = 'superfish/superfish_supposition';
855     }
856
857     if ($this->configuration['supersubs']) {
858       $build['#attached']['library'][] = 'superfish/superfish_supersubs';
859       $minwidth = $this->configuration['minwidth'];
860       $maxwidth = $this->configuration['maxwidth'];
861       $sfplugins['supersubs']['minWidth'] = ($minwidth != 12) ? $minwidth : '';
862       $sfplugins['supersubs']['maxWidth'] = ($maxwidth != 27) ? $maxwidth : '';
863       if (empty($sfplugins['supersubs']['minWidth']) && empty($sfplugins['supersubs']['maxWidth'])) {
864         $sfplugins['supersubs'] = TRUE;
865       }
866     }
867
868     // Attaching the requires JavaScript and CSS files.
869     $build['#attached']['library'][] = 'superfish/superfish';
870     if ($sfsettings['style'] != 'none') {
871       $build['#attached']['library'][] = 'superfish/superfish_style_'. $sfsettings['style'];
872     }
873
874     // Title for the small-screen menu.
875     if ($smallscreen) {
876       $title = '';
877       switch ($type) {
878         case 0 :
879           $title = $this->configuration['smallset'];
880         break;
881         case 1 :
882           $title = $this->configuration['smallamt'];
883         break;
884       }
885       $sfplugins['smallscreen']['title'] = $title ? $title : $this->label();
886     }
887     $sfplugins = superfish_array_remove_empty($sfplugins);
888
889     // Menu block ID.
890     $menu_name = $this->getDerivativeId();
891
892     // Menu tree.
893     $level = $this->configuration['level'];
894     // Menu display depth.
895     $depth = $sfsettings['depth'];
896
897     // By not setting the any expanded parents we don't limit the loading of the subtrees.
898     // Calling MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters we would be
899     // doing so. We don't actually need the parents expanded as we do different rendering.
900     $parameters = (new MenuTreeParameters())
901       ->setMinDepth($level)
902       ->setMaxDepth($depth ? min($level + ($depth - 1), $this->menuTree->maxDepth()) : NULL)
903       ->setActiveTrail($this->menuActiveTrail->getActiveTrailIds($menu_name))
904       ->onlyEnabledLinks();
905
906     $tree = $this->menuTree->load($menu_name, $parameters);
907     $manipulators = array(
908       ['callable' => 'menu.default_tree_manipulators:checkAccess'],
909       ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']
910     );
911     $tree = $this->menuTree->transform($tree, $manipulators);
912
913     // Unique HTML ID.
914     $html_id = Html::getUniqueId('superfish-' . $menu_name);
915
916     // Preparing the Drupal behavior.
917     $build['#attached']['drupalSettings']['superfish'][$html_id]['id'] = $html_id;
918     $build['#attached']['drupalSettings']['superfish'][$html_id]['sf'] = isset($sfoptions) ? $sfoptions : array();
919     if (!empty($sfplugins)) {
920       $build['#attached']['drupalSettings']['superfish'][$html_id]['plugins'] = $sfplugins;
921     }
922
923     // Calling the theme.
924     $build['content'] = array(
925       '#theme'  => 'superfish',
926       '#menu_name' => $menu_name,
927       '#html_id' => $html_id,
928       '#tree' => $tree,
929       '#settings' => $sfsettings
930     );
931     // Build the original menu tree to calculate cache tags and contexts.
932     $treeBuild = $this->menuTree->build($tree);
933     $build['#cache'] = $treeBuild['#cache'];
934
935     return $build;
936   }
937
938   /**
939    * Overrides \Drupal\block\BlockBase::defaultConfiguration().
940    */
941   public function defaultConfiguration() {
942     return parent::defaultConfiguration() + [
943       'level' => 1,
944       'depth' => 0,
945       'menu_type' => 'horizontal',
946       'style' => 'none',
947       'arrow' => 1,
948       'shadow' => 1,
949       'speed' => 'fast',
950       'delay' => 800,
951       'slide' => 'vertical',
952       'supposition' => 1,
953       'hoverintent' => 1,
954       'touch' => 0,
955       'touchbh' => 2,
956       'touchbp' => 768,
957       'touchua' => 0,
958       'touchual' => '',
959       'touchuam' => 0,
960       'small' => 2,
961       'smallbp' => 768,
962       'smallua' => 0,
963       'smallual' => '',
964       'smalluam' => 0,
965       'smallact' => 1,
966       'smallset' => '',
967       'smallasa' => 0,
968       'smallcmc' => 0,
969       'smallecm' => '',
970       'smallchc' => 0,
971       'smallech' => '',
972       'smallicm' => '',
973       'smallich' => '',
974       'smallamt' => '',
975       'smallabt' => 1,
976       'supersubs' => 1,
977       'minwidth' => 12,
978       'maxwidth' => 27,
979       'multicolumn' => 0,
980       'multicolumn_depth' => 1,
981       'multicolumn_levels' => 0,
982       'pathlevels' => 1,
983       'expanded' => 0,
984       'clone_parent' => 0,
985       'hide_linkdescription' => 0,
986       'add_linkdescription' => 0,
987       'link_depth_class' => 1,
988       'custom_list_class' => '',
989       'custom_item_class' => '',
990       'custom_link_class' => ''
991     ];
992   }
993
994 }