-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowlcarousel2.module
138 lines (122 loc) · 4.56 KB
/
owlcarousel2.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @file
* Contains owlcarousel2.module.
*/
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\owlcarousel2\Util;
/**
* Implements hook_help().
*/
function owlcarousel2_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the owlcarousel2 module.
case 'help.page.owlcarousel2':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('OwlCarousel2 Implementation, full featured with youtube support, and tons of slide effects.') . '</p>';
$output .= '<p>' . t('See the script repository on <a href="https://github.com/OwlCarousel2/OwlCarousel2">Github</a>') . '</p>';
$output .= '<p>' . t('<a href="https://github.com/OwlCarousel2/OwlCarousel2/archive/2.3.4.zip">Click here to download the script</a> and copy the <strong><code>dist</code></strong> folder under <strong><code>{Drupal Installation}/libraries/OwlCarousel2</code></strong>') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function owlcarousel2_theme() {
$themes = [
'owlcarousel2' => [
'render element' => 'elements',
],
'owlcarousel2_image_item' => [
'render element' => 'elements',
],
'owlcarousel2_custom_text' => [
'render element' => 'elements',
],
'owlcarousel2_node' => [
'template' => 'owlcarousel2-node',
'base hook' => 'node',
],
'owlcarousel2_block' => [
'variables' => [
'content' => NULL,
'id' => NULL,
'nav_titles' => NULL,
'carousel_navigation' => NULL,
'nav_ratio' => NULL,
'nav_height' => NULL,
'nav_width' => NULL,
],
'template' => 'block--owlcarousel2',
],
];
return $themes;
}
/**
* Implements hook_preprocess_HOOK().
*/
function owlcarousel2_preprocess_owlcarousel2(&$variables) {
$elements = $variables['elements'];
/** @var \Drupal\owlcarousel2\Entity\OwlCarousel2 $carousel */
$carousel = $elements['#owlcarousel2'];
try {
$data = Util::getCarouselData($carousel->id());
}
catch (EntityMalformedException $e) {
return $e;
}
$content = $data['content'];
$nav_titles = $data['navigation_titles'];
$nav_ratio = $data['nav_ratio'];
$nav_height = $data['nav_height'];
$nav_width = $data['nav_width'];
$settings = $carousel->get('settings')->getValue()[0];
$carousel_navigation = isset($settings['carouselNavigation']) ? ($settings['carouselNavigation'] == 'true') : FALSE;
// If it is a carousel navigation, include a setting navigation size and set
// the items per slide to 1.
if ($carousel_navigation) {
$settings['carouselNavigationSize'] = $settings['items_per_slide'];
$settings['items_per_slide'] = 1;
}
$variables['content']['#markup'] = Markup::create($content);
$variables['id'] = $carousel->id();
$variables['nav_titles'] = $nav_titles;
$variables['carousel_navigation'] = $carousel_navigation;
$variables['nav_ratio'] = $nav_ratio;
$variables['nav_height'] = $nav_height;
$variables['nav_width'] = $nav_width;
$variables['#attached']['library'][] = 'owlcarousel2/owlcarousel2';
$container = \Drupal::getContainer();
/** @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value */
$key_value = $container->get('keyvalue.expirable');
$key_value->get('owlcarousel2')->set($carousel->id(), $settings);
$keyed_settings = $key_value->get('owlcarousel2')->getAll();
$variables['#attached']['drupalSettings']['owlcarousel_settings'] = $keyed_settings;
}
/**
* Implements hook_preprocess_HOOK().
*/
function owlcarousel2_preprocess_html(&$variables) {
// Clear the carousel key/par settings to avoid garbage.
$container = \Drupal::getContainer();
/** @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value */
$key_value = $container->get('keyvalue.expirable');
$key_value->get('owlcarousel2')->deleteAll();
}
/**
* Implements hook_theme_suggestions_node_alter().
*/
function owlcarousel2_theme_suggestions_node_alter(array &$suggestions, array $variables) {
// Make the owlcarousel2_node template the weakest choice.
if (in_array('owlcarousel2_node', $suggestions)) {
$key = array_search('owlcarousel2_node', $suggestions);
if ($key) {
array_unshift($suggestions, $suggestions[$key]);
unset($suggestions[$key]);
}
}
}