comparison modules/contrib/views_slideshow/views_slideshow.theme.inc @ 5:c69a71b4f40f

Add slideshow module
author Chris Cannam
date Thu, 07 Dec 2017 14:46:23 +0000
parents
children
comparison
equal deleted inserted replaced
4:8948ab6c87d2 5:c69a71b4f40f
1 <?php
2
3 /**
4 * @file
5 * The theme system, which controls the output of views slideshow.
6 */
7
8 /**
9 * @defgroup vss_templates Templates
10 * @{
11 * Slideshow and component templates.
12 *
13 * @see vss_theme
14 * @}
15 */
16
17 /**
18 * @defgroup vss_theme Theme Functions
19 * @{
20 * Theme processing and display generation.
21 *
22 * Most of the logic behind the generation of the slideshow is here.
23 *
24 * @see vss_templates
25 */
26
27 use Drupal\Component\Utility\Html;
28
29 /**
30 * Views Slideshow: slideshow.
31 */
32 function _views_slideshow_preprocess_views_view_slideshow(&$vars) {
33 $options = $vars['view']->style_plugin->options;
34 $vars['skin'] = 'default';
35 $vars['slideshow'] = '';
36 $main_frame_module = $options['slideshow_type'];
37
38 if (empty($main_frame_module)) {
39 // Get all slideshow types.
40 $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
41 $types = $typeManager->getDefinitions();
42
43 if ($types) {
44 foreach ($types as $id => $definition) {
45 $main_frame_module = $id;
46 break;
47 }
48 }
49 }
50
51 // Make sure the main slideshow settings are defined before building the
52 // slideshow.
53 if (empty($main_frame_module)) {
54 drupal_set_message(
55 t('No main frame module is enabled for views slideshow. This is often because another module which Views Slideshow needs is not enabled. For example, 4.x needs a module like "Views Slideshow: Cycle" enabled.'),
56 'error'
57 );
58 }
59 elseif (empty($options[$main_frame_module])) {
60 drupal_set_message(t('The options for @module does not exists.', ['@module' => $main_frame_module]), 'error');
61 }
62 elseif (!empty($vars['rows'])) {
63 $settings = $options[$main_frame_module];
64 $view = $vars['view'];
65 $rows = $vars['rows'];
66
67 // The #name element is not available on Views edit pages.
68 $view_element_name = (isset($view->element['#name'])) ? $view->element['#name'] : '';
69 $vss_id = $view_element_name . '-' . $view->current_display;
70
71 // Give each slideshow a unique id if there are more than one on the page.
72 static $instances = [];
73 if (isset($instances[$vss_id])) {
74 $instances[$vss_id]++;
75 $vss_id .= "_" . $instances[$vss_id];
76 }
77 else {
78 $instances[$vss_id] = 1;
79 }
80
81 // Building our default methods.
82 $methods = [
83 'goToSlide' => [],
84 'nextSlide' => [],
85 'pause' => [],
86 'play' => [],
87 'previousSlide' => [],
88 'transitionBegin' => [],
89 'transitionEnd' => [],
90 ];
91
92 // Pull all widget info and slideshow info and merge them together.
93 $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
94 $widgetTypes = $widgetTypeManager->getDefinitions();
95
96 $slideshowTypeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
97 $slideshowTypes = $slideshowTypeManager->getDefinitions();
98
99 $addons = array_merge($widgetTypes, $slideshowTypes);
100
101 // Loop through all the addons and call their methods if needed.
102 foreach ($addons as $addon_id => $addon_info) {
103 foreach ($addon_info['accepts'] as $imp_key => $imp_value) {
104 if (is_array($imp_value)) {
105 $methods[$imp_key][] = \Drupal::service('views_slideshow.format_addons_name')->format($addon_id);
106 }
107 else {
108 $methods[$imp_value][] = \Drupal::service('views_slideshow.format_addons_name')->format($addon_id);
109 }
110 }
111 }
112
113 $vars['#attached']['library'][] = 'views_slideshow/widget_info';
114 $vars['#attached']['drupalSettings']['viewsSlideshow'][$vss_id] = [
115 'methods' => $methods,
116 'paused' => 0,
117 ];
118
119 // Process Skins.
120 $skinManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_skin');
121 $skin = $skinManager->createInstance($options['slideshow_skin']);
122
123 $vars['skin'] = $skin->getClass();
124
125 foreach ($skin->getLibraries() as $library) {
126 $vars['#attached']['library'][] = $library;
127 }
128
129 // Process Widgets.
130 // Build weights.
131 $weight = [];
132 for ($i = 1; $i <= count($widgetTypes); $i++) {
133 $weight['top'][$i] = [];
134 $weight['bottom'][$i] = [];
135 }
136
137 $slide_count = count($view->result);
138 if ($slide_count && $vars['view']->style_plugin->options['slideshow_type'] == 'views_slideshow_cycle') {
139 $items_per_slide = $vars['view']->style_plugin->options['views_slideshow_cycle']['items_per_slide'];
140 $slide_count = $slide_count / $items_per_slide;
141 }
142 foreach ($widgetTypes as $widgetTypeId => $widgetTypeName) {
143 foreach ($weight as $location => $order) {
144 if ($options['widgets'][$location][$widgetTypeId]['enable']) {
145 // If hide on single slide and only a single slide skip rendering.
146 if ($options['widgets'][$location][$widgetTypeId]['hide_on_single_slide'] && $slide_count <= 1) {
147 continue;
148 }
149 $widgetWeight = ($options['widgets'][$location][$widgetTypeId]['weight'] > count($widgetTypes)) ? count($widgetTypes) : $options['widgets'][$location][$widgetTypeId]['weight'];
150
151 $weight[$location][$widgetWeight][] = [
152 'widgetId' => $widgetTypeId,
153 'widgetSettings' => $options['widgets'][$location][$widgetTypeId],
154 ];
155 }
156 }
157 }
158
159 // Build our widgets.
160 foreach ($weight as $location => $order) {
161 $vars[$location . '_widget_rendered'] = [];
162 foreach ($order as $widgets) {
163 if (is_array($widgets)) {
164 foreach ($widgets as $widgetData) {
165 $vars[$location . '_widget_rendered'][] = [
166 '#theme' => $view->buildThemeFunctions($widgetData['widgetId'] . '_widget_render'),
167 '#vss_id' => $vss_id,
168 '#view' => $view,
169 '#settings' => $widgetData['widgetSettings'],
170 '#location' => $location,
171 '#rows' => $rows,
172 ];
173 }
174 }
175 }
176 }
177
178 // Process Slideshow.
179 $slides = [
180 '#theme' => $view->buildThemeFunctions($main_frame_module . '_main_frame'),
181 '#vss_id' => $vss_id,
182 '#view' => $view,
183 '#settings' => $settings,
184 '#rows' => $rows,
185 ];
186
187 $vars['slideshow'] = [
188 '#theme' => $view->buildThemeFunctions('views_slideshow_main_section'),
189 '#vss_id' => $vss_id,
190 '#slides' => $slides,
191 '#plugin' => $main_frame_module,
192 ];
193 }
194 }
195
196 /**
197 * Views Slideshow: pager.
198 */
199 function template_preprocess_views_slideshow_pager_widget_render(&$vars) {
200 // Add JavaScript settings for the pager type.
201 $vars['#attached']['library'][] = 'views_slideshow/widget_info';
202 $vars['#attached']['drupalSettings']['viewsSlideshowPager'][$vars['vss_id']] = [
203 $vars['location'] => [
204 'type' => \Drupal::service('views_slideshow.format_addons_name')->format($vars['settings']['type']),
205 ],
206 ];
207
208 // Create some attributes.
209 $attributes['class'][] = 'widget_pager widget_pager_' . $vars['location'];
210 $attributes['id'] = 'widget_pager_' . $vars['location'] . '_' . $vars['vss_id'];
211
212 $pager = [
213 '#theme' => $vars['view']->buildThemeFunctions($vars['settings']['type']),
214 '#vss_id' => $vars['vss_id'],
215 '#view' => $vars['view'],
216 '#settings' => $vars['settings'],
217 '#location' => $vars['location'],
218 '#attributes' => $attributes,
219 ];
220
221 return \Drupal::service('renderer')->render($pager);
222 }
223
224 /**
225 * Theme pager fields.
226 */
227 function template_preprocess_views_slideshow_pager_fields(&$vars) {
228 // Add JavaScript settings for the field.
229 $vars['#attached']['library'][] = 'views_slideshow/widget_info';
230 $vars['#attached']['drupalSettings']['viewsSlideshowPagerFields'][$vars['vss_id']] = [
231 $vars['location'] => [
232 'activatePauseOnHover' => $vars['settings']['views_slideshow_pager_fields']['views_slideshow_pager_fields_hover'],
233 ],
234 ];
235
236 // Add hover intent library.
237 if ($vars['settings']['views_slideshow_pager_fields']['views_slideshow_pager_fields_hover']) {
238 $vars['#attached']['library'][] = 'views_slideshow/jquery_hoverIntent';
239 }
240
241 $vars['widget_id'] = $vars['attributes']['id'];
242 // Add our class to the wrapper.
243 $vars['attributes']['class'][] = 'views_slideshow_pager_field';
244
245 // Render all the fields unless there is only 1 slide and the user specified
246 // to hide them when there is only one slide.
247 $vars['rendered_field_items'] = [];
248 foreach ($vars['view']->result as $count => $node) {
249 $rendered_fields = [];
250 foreach ($vars['settings']['views_slideshow_pager_fields']['views_slideshow_pager_fields_fields'] as $field => $use) {
251 if ($use !== 0 && is_object($vars['view']->field[$field])) {
252 $rendered_fields[] = [
253 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_pager_field_field'),
254 '#view' => $vars['view'],
255 '#label' => $vars['view']->field[$field]->options['label'],
256 '#output' => $vars['view']->style_plugin->getField($count, $field),
257 '#css_identifier' => Html::cleanCssIdentifier($vars['view']->field[$field]->field),
258 ];
259 }
260 }
261
262 $vars['rendered_field_items'][] = [
263 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_pager_field_item'),
264 '#vss_id' => $vars['vss_id'],
265 '#item' => $rendered_fields,
266 '#count' => $count,
267 '#location' => $vars['location'],
268 '#length' => count($vars['view']->result),
269 ];
270 }
271 }
272
273 /**
274 * Views Slideshow: pager item.
275 */
276 function template_preprocess_views_slideshow_pager_field_item(&$vars) {
277 $vars['attributes']['class'][] = 'views_slideshow_pager_field_item';
278 $vars['attributes']['class'][] = ($vars['count'] % 2) ? 'views-row-even' : 'views-row-odd';
279 if ($vars['count'] == 0) {
280 $vars['attributes']['class'][] = 'views-row-first';
281 }
282 elseif ($vars['count'] == $vars['length'] - 1) {
283 $vars['attributes']['class'][] = 'views-row-last';
284 }
285 }
286
287 /**
288 * Views Slideshow: Bullets pager.
289 */
290 function template_preprocess_views_slideshow_pager_bullets(&$vars) {
291 $vars['#attached']['library'][] = 'views_slideshow/widget_info';
292 $vars['#attached']['library'][] = 'views_slideshow/pager_bullets';
293 $vars['#attached']['drupalSettings']['viewsSlideshowPagerFields'][$vars['vss_id']] = [
294 $vars['location'] => [
295 'activatePauseOnHover' => $vars['settings']['views_slideshow_pager_bullets']['views_slideshow_pager_bullets_hover'],
296 ],
297 ];
298
299 $vars['bullet_items'] = [
300 '#theme' => 'item_list',
301 '#items' => [],
302 '#attributes' => $vars['attributes'],
303 ];
304 $vars['bullet_items']['#attributes']['class'][] = 'views-slideshow-pager-bullets';
305 $vars['bullet_items']['#attributes']['class'][] = 'views_slideshow_pager_field';
306
307 for ($i = 0; $i < count($vars['view']->result); $i++) {
308 $vars['bullet_items']['#items'][] = [
309 '#markup' => $i,
310 '#wrapper_attributes' => [
311 'id' => 'views_slideshow_pager_field_item_' . $vars['location'] . '_' . $vars['vss_id'] . '_' . $i,
312 ],
313 ];
314 }
315 }
316
317 /**
318 * Views Slideshow: Controls.
319 */
320 function template_preprocess_views_slideshow_controls_widget_render(&$vars) {
321 // Add JavaScript settings for the controls type.
322 $vars['#attached']['library'][] = 'views_slideshow/widget_info';
323 $vars['#attached']['drupalSettings']['viewsSlideshowControls'][$vars['vss_id']] = [
324 $vars['location'] => [
325 'type' => \Drupal::service('views_slideshow.format_addons_name')->format($vars['settings']['type']),
326 ],
327 ];
328
329 $output = [
330 '#theme' => $vars['view']->buildThemeFunctions($vars['settings']['type']),
331 '#vss_id' => $vars['vss_id'],
332 '#view' => $vars['view'],
333 '#settings' => $vars['settings'],
334 '#location' => $vars['location'],
335 '#rows' => $vars['rows'],
336 ];
337
338 return \Drupal::service('renderer')->render($output);
339 }
340
341 /**
342 * The slideshow controls.
343 */
344 function template_preprocess_views_slideshow_controls_text(&$vars) {
345 $vars['#attached']['library'][] = 'views_slideshow/controls_text';
346
347 $vars['attributes']['class'][] = 'views_slideshow_controls_text';
348
349 $vars['rendered_control_previous'] = [
350 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_controls_text_previous'),
351 '#vss_id' => $vars['vss_id'],
352 '#view' => $vars['view'],
353 '#settings' => $vars['settings'],
354 ];
355
356 $vars['rendered_control_pause'] = [
357 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_controls_text_pause'),
358 '#vss_id' => $vars['vss_id'],
359 '#view' => $vars['view'],
360 '#settings' => $vars['settings'],
361 ];
362
363 $vars['rendered_control_next'] = [
364 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_controls_text_next'),
365 '#vss_id' => $vars['vss_id'],
366 '#view' => $vars['view'],
367 '#settings' => $vars['settings'],
368 ];
369 }
370
371 /**
372 * Views Slideshow: "previous" control.
373 */
374 function template_preprocess_views_slideshow_controls_text_previous(&$vars) {
375 $vars['attributes']['class'][] = 'views_slideshow_controls_text_previous';
376 }
377
378 /**
379 * Views Slideshow: "pause" control.
380 */
381 function template_preprocess_views_slideshow_controls_text_pause(&$vars) {
382 $vars['attributes']['class'][] = 'views_slideshow_controls_text_pause views-slideshow-controls-text-status-play';
383 $vars['start_text'] = t('Pause');
384 }
385
386 /**
387 * Views Slideshow: "next" control.
388 */
389 function template_preprocess_views_slideshow_controls_text_next(&$vars) {
390 $vars['attributes']['class'][] = 'views_slideshow_controls_text_next';
391 }
392
393 /**
394 * Views Slideshow: Slide Counter.
395 */
396 function template_preprocess_views_slideshow_slide_counter_widget_render(&$vars) {
397 $slide = [
398 '#theme' => $vars['view']->buildThemeFunctions('views_slideshow_slide_counter'),
399 '#vss_id' => $vars['vss_id'],
400 '#view' => $vars['view'],
401 '#settings' => $vars['settings'],
402 '#location' => $vars['location'],
403 '#rows' => $vars['rows'],
404 ];
405
406 return \Drupal::service('renderer')->render($slide);
407 }
408
409 /**
410 * Views Slideshow: slide counter.
411 */
412 function template_preprocess_views_slideshow_slide_counter(&$vars) {
413 $vars['attributes']['class'][] = 'views_slideshow_slide_counter';
414 $vars['slide_count'] = count($vars['rows']);
415 }
416
417 /**
418 * @} End of "defgroup vss_theme".
419 */