comparison modules/contrib/views_slideshow/src/Plugin/views/style/Slideshow.php @ 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 namespace Drupal\views_slideshow\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\style\StylePluginBase;
7 use Drupal\Core\Url;
8 use Drupal\Core\Link;
9
10 /**
11 * Style plugin to render each item in a slideshow.
12 *
13 * @ingroup views_style_plugins
14 *
15 * @ViewsStyle(
16 * id = "slideshow",
17 * title = @Translation("Slideshow"),
18 * help = @Translation("Display the results as a slideshow."),
19 * theme = "views_view_slideshow",
20 * display_types = {"normal"}
21 * )
22 */
23 class Slideshow extends StylePluginBase {
24
25 /**
26 * Does the style plugin allows to use style plugins.
27 *
28 * @var bool
29 */
30 protected $usesRowPlugin = TRUE;
31
32 /**
33 * Does the style plugin support custom css class for the rows.
34 *
35 * @var bool
36 */
37 protected $usesRowClass = TRUE;
38
39 /**
40 * Does the style plugin support grouping of rows.
41 *
42 * @var bool
43 */
44 protected $usesGrouping = FALSE;
45
46 /**
47 * Does the style plugin for itself support to add fields to it's output.
48 *
49 * This option only makes sense on style plugins without row plugins, like
50 * for example table.
51 *
52 * @var bool
53 */
54 protected $usesFields = TRUE;
55
56 /**
57 * {@inheritdoc}
58 */
59 protected function defineOptions() {
60 $options = parent::defineOptions();
61 $options['row_class_custom'] = ['default' => ''];
62 $options['row_class_default'] = ['default' => TRUE];
63 $options['slideshow_type'] = ['default' => 'views_slideshow_cycle'];
64 $options['slideshow_skin'] = ['default' => 'default'];
65
66 $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
67 foreach ($typeManager->getDefinitions() as $id => $definition) {
68 $instance = $typeManager->createInstance($id, []);
69 $options[$id] = $instance->defaultConfiguration();
70 }
71
72 $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
73 $widgetTypes = $widgetTypeManager->getDefinitions();
74 foreach (['top', 'bottom'] as $location) {
75 foreach ($widgetTypes as $widgetTypeId => $widgetTypeDefinition) {
76 $options['widgets']['contains'][$location]['contains'][$widgetTypeId]['contains'] = $widgetTypeManager->createInstance($widgetTypeId, [])->defaultConfiguration();
77 }
78 }
79
80 return $options;
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
87 parent::buildOptionsForm($form, $form_state);
88
89 // Wrap all the form elements to help style the form.
90 $form['views_slideshow_wrapper'] = [
91 '#markup' => '<div id="views-slideshow-form-wrapper">',
92 ];
93
94 // Skins.
95 $form['slideshow_skin_header'] = [
96 '#markup' => '<h2>' . $this->t('Style') . '</h2>',
97 ];
98
99 /* @var \Drupal\Component\Plugin\PluginManagerInterface */
100 $skinManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_skin');
101
102 // Get all skins to create the option list.
103 $skins = [];
104 foreach ($skinManager->getDefinitions() as $id => $definition) {
105 $skins[$id] = $definition['label'];
106 }
107 asort($skins);
108
109 // Create the drop down box so users can choose an available skin.
110 $form['slideshow_skin'] = [
111 '#type' => 'select',
112 '#title' => $this->t('Skin'),
113 '#options' => $skins,
114 '#default_value' => $this->options['slideshow_skin'],
115 '#description' => $this->t('Select the skin to use for this display. Skins allow for easily swappable layouts of things like next/prev links and thumbnails. Note that not all skins support all widgets, so a combination of skins and widgets may lead to unpredictable results in layout.'),
116 ];
117
118 // Slides.
119 $form['slides_header'] = [
120 '#markup' => '<h2>' . $this->t('Slides') . '</h2>',
121 ];
122
123 // Get all slideshow types.
124 $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
125 $types = $typeManager->getDefinitions();
126
127 if ($types) {
128
129 // Build our slideshow options for the form.
130 $slideshow_options = [];
131 foreach ($types as $id => $definition) {
132 $slideshow_options[$id] = $definition['label'];
133 }
134
135 $form['slideshow_type'] = [
136 '#type' => 'select',
137 '#title' => $this->t('Slideshow Type'),
138 '#options' => $slideshow_options,
139 '#default_value' => $this->options['slideshow_type'],
140 ];
141
142 // @todo: check if default values are properly passed to the buildConfigurationForm().
143 foreach ($types as $id => $definition) {
144 $configuration = [];
145 if (!empty($this->options[$id])) {
146 $configuration = $this->options[$id];
147 }
148 $instance = $typeManager->createInstance($id, $configuration);
149
150 $form[$id] = [
151 '#type' => 'fieldset',
152 '#title' => $this->t('@module options', ['@module' => $definition['label']]),
153 '#collapsible' => TRUE,
154 '#attributes' => ['class' => [$id]],
155 '#states' => [
156 'visible' => [
157 ':input[name="style_options[slideshow_type]"]' => ['value' => $id],
158 ],
159 ],
160 ];
161
162 $form = $instance->buildConfigurationForm($form, $form_state);
163 }
164 }
165 else {
166 $form['enable_module'] = [
167 '#markup' => $this->t('There is no Views Slideshow plugin enabled. Go to the @modules and enable a Views Slideshow plugin module. For example Views Slideshow Cycle.', ['@modules' => Link::fromTextAndUrl($this->t('Modules Page'), Url::fromRoute('system.modules_list'))->toString()]),
168 ];
169 }
170
171 // Widgets.
172 // @todo: Improve the UX by using Ajax.
173 $form['widgets_header'] = [
174 '#markup' => '<h2>' . $this->t('Widgets') . '</h2>',
175 ];
176
177 // Define the available locations.
178 $location = ['top' => $this->t('Top'), 'bottom' => $this->t('Bottom')];
179
180 // Loop through all locations so we can add header for each location.
181 foreach ($location as $location_id => $location_name) {
182 $form['widgets'][$location_id]['header'] = [
183 '#markup' => '<h3>' . $this->t('@location Widgets', ['@location' => $location_name]) . '</h3>',
184 ];
185 }
186
187 /* @var \Drupal\Component\Plugin\PluginManagerInterface */
188 $widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
189
190 // Get all widgets types that are registered.
191 $widgets = $widgetTypeManager->getDefinitions();
192 if (!empty($widgets)) {
193
194 // Build our weight values by number of widgets.
195 $weights = [];
196 for ($i = 1; $i <= count($widgets); $i++) {
197 $weights[$i] = $i;
198 }
199
200 // Loop through our widgets and locations to build our form values for
201 // each widget.
202 foreach ($widgets as $widget_id => $widget_info) {
203
204 // Determine if this widget type is compatible with any slideshow type.
205 $compatible_slideshows = [];
206 foreach ($types as $slideshow_id => $slideshow_info) {
207 if ($widgetTypeManager->createInstance($widget_id, [])->checkCompatiblity($slideshow_info)) {
208 $compatible_slideshows[] = $slideshow_id;
209 }
210 }
211
212 // Display the widget config form only if the widget type is compatible
213 // with at least one slideshow type.
214 if (!empty($compatible_slideshows)) {
215 foreach ($location as $location_id => $location_name) {
216 // Use Widget Checkbox.
217 $form['widgets'][$location_id][$widget_id]['enable'] = [
218 '#type' => 'checkbox',
219 '#title' => $widget_info['label'],
220 '#default_value' => $this->options['widgets'][$location_id][$widget_id]['enable'],
221 '#description' => $this->t('Should @name be rendered at the @location of the slides.', ['@name' => $widget_info['label'], '@location' => $location_name]),
222 '#dependency' => [
223 'edit-style-options-slideshow-type' => $compatible_slideshows,
224 ],
225 ];
226
227 // Need to wrap this so it indents correctly.
228 $form['widgets'][$location_id][$widget_id]['wrapper'] = [
229 '#markup' => '<div class="vs-dependent">',
230 ];
231
232 // Widget weight.
233 // We check to see if the default value is greater than the number
234 // of widgets just in case a widget has been removed and the form
235 // hasn't been saved again.
236 $weight = (isset($this->options['widgets'][$location_id][$widget_id]['weight'])) ? $this->options['widgets'][$location_id][$widget_id]['weight'] : 0;
237 if ($weight > count($widgets)) {
238 $weight = count($widgets);
239 }
240 $form['widgets'][$location_id][$widget_id]['weight'] = [
241 '#type' => 'select',
242 '#title' => $this->t('Weight of the @name', ['@name' => $widget_info['label']]),
243 '#default_value' => $weight,
244 '#options' => $weights,
245 '#description' => $this->t('Determines in what order the @name appears. A lower weight will cause the @name to be above higher weight items.', ['@name' => $widget_info['label']]),
246 '#prefix' => '<div class="vs-dependent">',
247 '#suffix' => '</div>',
248 '#states' => [
249 'visible' => [
250 ':input[name="style_options[widgets][' . $location_id . '][' . $widget_id . '][enable]"]' => ['checked' => TRUE],
251 ],
252 ],
253 ];
254
255 // Build the appropriate array for the states API.
256 $widget_dependency = 'style_options[widgets][' . $location_id . '][' . $widget_id . ']';
257
258 // Get the current configuration of this widget type.
259 $configuration = [];
260 if (!empty($this->options['widgets'][$location_id][$widget_id])) {
261 $configuration = $this->options['widgets'][$location_id][$widget_id];
262 }
263 $configuration['dependency'] = $widget_dependency;
264 $instance = $widgetTypeManager->createInstance($widget_id, $configuration);
265
266 // Get the configuration form of this widget type.
267 $form['widgets'][$location_id][$widget_id] = $instance->buildConfigurationForm($form['widgets'][$location_id][$widget_id], $form_state);
268
269 // Close the vs-dependent wrapper.
270 $form['widgets'][$location_id][$widget_id]['wrapper_close'] = [
271 '#markup' => '</div>',
272 ];
273 }
274 }
275 }
276 }
277
278 // Browse locations and remove the header if no widget is available.
279 foreach ($location as $location_id => $location_name) {
280 // If no widget is available, the only key is "header".
281 if (count(array_keys($form['widgets'][$location_id])) == 1) {
282 unset($form['widgets'][$location_id]);
283 }
284 }
285
286 // Remove the widget section header if there is no widget available.
287 if (empty($form['widgets'])) {
288 unset($form['widgets']);
289 unset($form['widgets_header']);
290 }
291
292 $form['views_slideshow_wrapper_close'] = [
293 '#markup' => '</div>',
294 ];
295
296 // Add a library to style the form.
297 $form['#attached']['library'] = ['views_slideshow/form'];
298 }
299
300 /**
301 * {@inheritdoc}
302 */
303 public function validateOptionsForm(&$form, FormStateInterface $form_state) {
304 // Validate all slideshow type plugins values.
305 $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
306 foreach ($typeManager->getDefinitions() as $id => $definition) {
307 $type = $typeManager->createInstance($id);
308 $type->validateConfigurationForm($form, $form_state);
309 }
310 }
311
312 /**
313 * {@inheritdoc}
314 */
315 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
316 // Submit all slideshow type plugins values.
317 $typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
318 foreach ($typeManager->getDefinitions() as $id => $definition) {
319 $type = $typeManager->createInstance($id);
320 $type->submitConfigurationForm($form, $form_state);
321 }
322 }
323
324 }