-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexslider_views.module
77 lines (65 loc) · 2.78 KB
/
flexslider_views.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* @file
* A light-weight, customizable lightbox plugin for jQuery 1.3
*/
use Drupal\Core\Template\Attribute;
use Drupal\Component\Serialization\Json;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
/**
* Prepares variables for Views Flexslider templates.
*
* Default template: views-view-flexslider.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A View object.
*/
function template_preprocess_views_view_flexslider(&$variables) {
/** @var \Drupal\flexslider_views\Plugin\views\style\FlexSlider $handler */
$handler = $variables['view']->style_plugin;
// Fetch classes from handler options.
$list_classes = [];
if ($handler->options['class']) {
$list_classes = explode(' ', $handler->options['class']);
$list_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', $list_classes);
}
$variables['list']['attributes'] = new Attribute(array('class' => $list_classes));
// Fetch wrapper classes from handler options.
$wrapper_classes = ['flexslider'];
if (!empty($handler->options['wrapper_class'])) {
$wrapper_classes = array_merge($wrapper_classes, explode(' ', $handler->options['wrapper_class']));
$wrapper_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', $wrapper_classes);
}
$variables['attributes'] = new Attribute(array('class' => $wrapper_classes));
$variables['list']['type'] = $handler->options['type'];
if ($handler->options['flexslide_controlNav'] == 'thumbnails' && !empty($handler->options['flexslide_thumbField'])) {
$thumbField = $variables['view']->field[$handler->options['flexslide_thumbField']];
$thumbImageStyle = $handler->options['flexslide_thumbImageStyle'];
}
// Flexslider configuration.
$variables['settings'] = $handler->filterOptions();
$variables['attributes']['data-flexsliderconfig'] = Json::encode($variables['settings']);
$variables['#attached']['library'][] = 'flexslider_views/flexslider_views';
template_preprocess_views_view_unformatted($variables);
// Attributes are created in template_preprocess_views_view_unformatted().
// Add data-thumb attribute for thumbnail ControlNav.
if (isset($thumbField)) {
foreach ($variables['rows'] as $id => $row) {
$fid = $row['content']['#row']->{$thumbField->aliases[$thumbField->realField]};
if (is_numeric($fid) && $file = File::load($fid)) {
$image_uri = $file->getFileUri();
// Get image style URL
if ($thumbImageStyle) {
$image_uri = ImageStyle::load($thumbImageStyle)->buildUrl($image_uri);
}
else {
// Get absolute path for original image
$image_uri = $file->url();
}
$variables['rows'][$id]['attributes']->setAttribute('data-thumb', $image_uri);
}
}
}
}