View source
<?php
namespace Drupal\views_slideshow\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Url;
use Drupal\Core\Link;
class Slideshow extends StylePluginBase {
protected $usesRowPlugin = TRUE;
protected $usesRowClass = TRUE;
protected $usesGrouping = FALSE;
protected $usesFields = TRUE;
protected function defineOptions() {
$options = parent::defineOptions();
$options['row_class_custom'] = [
'default' => '',
];
$options['row_class_default'] = [
'default' => TRUE,
];
$options['slideshow_type'] = [
'default' => 'views_slideshow_cycle',
];
$options['slideshow_skin'] = [
'default' => 'default',
];
$typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
foreach ($typeManager
->getDefinitions() as $id => $definition) {
$instance = $typeManager
->createInstance($id, []);
$options[$id] = $instance
->defaultConfiguration();
}
$widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
$widgetTypes = $widgetTypeManager
->getDefinitions();
foreach ([
'top',
'bottom',
] as $location) {
foreach ($widgetTypes as $widgetTypeId => $widgetTypeDefinition) {
$options['widgets']['contains'][$location]['contains'][$widgetTypeId]['contains'] = $widgetTypeManager
->createInstance($widgetTypeId, [])
->defaultConfiguration();
}
}
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['views_slideshow_wrapper'] = [
'#markup' => '<div id="views-slideshow-form-wrapper">',
];
$form['slideshow_skin_header'] = [
'#markup' => '<h2>' . $this
->t('Style') . '</h2>',
];
$skinManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_skin');
$skins = [];
foreach ($skinManager
->getDefinitions() as $id => $definition) {
$skins[$id] = $definition['label'];
}
asort($skins);
$form['slideshow_skin'] = [
'#type' => 'select',
'#title' => $this
->t('Skin'),
'#options' => $skins,
'#default_value' => $this->options['slideshow_skin'],
'#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.'),
];
$form['slides_header'] = [
'#markup' => '<h2>' . $this
->t('Slides') . '</h2>',
];
$typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
$types = $typeManager
->getDefinitions();
if ($types) {
$slideshow_options = [];
foreach ($types as $id => $definition) {
$slideshow_options[$id] = $definition['label'];
}
$form['slideshow_type'] = [
'#type' => 'select',
'#title' => $this
->t('Slideshow Type'),
'#options' => $slideshow_options,
'#default_value' => $this->options['slideshow_type'],
];
foreach ($types as $id => $definition) {
$configuration = [];
if (!empty($this->options[$id])) {
$configuration = $this->options[$id];
}
$instance = $typeManager
->createInstance($id, $configuration);
$form[$id] = [
'#type' => 'fieldset',
'#title' => $this
->t('@module options', [
'@module' => $definition['label'],
]),
'#collapsible' => TRUE,
'#attributes' => [
'class' => [
$id,
],
],
'#states' => [
'visible' => [
':input[name="style_options[slideshow_type]"]' => [
'value' => $id,
],
],
],
];
$form = $instance
->buildConfigurationForm($form, $form_state);
}
}
else {
$form['enable_module'] = [
'#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(),
]),
];
}
$form['widgets_header'] = [
'#markup' => '<h2>' . $this
->t('Widgets') . '</h2>',
];
$location = [
'top' => $this
->t('Top'),
'bottom' => $this
->t('Bottom'),
];
foreach ($location as $location_id => $location_name) {
$form['widgets'][$location_id]['header'] = [
'#markup' => '<h3>' . $this
->t('@location Widgets', [
'@location' => $location_name,
]) . '</h3>',
];
}
$widgetTypeManager = \Drupal::service('plugin.manager.views_slideshow.widget_type');
$widgets = $widgetTypeManager
->getDefinitions();
if (!empty($widgets)) {
$weights = [];
for ($i = 1; $i <= count($widgets); $i++) {
$weights[$i] = $i;
}
foreach ($widgets as $widget_id => $widget_info) {
$compatible_slideshows = [];
foreach ($types as $slideshow_id => $slideshow_info) {
if ($widgetTypeManager
->createInstance($widget_id, [])
->checkCompatiblity($slideshow_info)) {
$compatible_slideshows[] = $slideshow_id;
}
}
if (!empty($compatible_slideshows)) {
foreach ($location as $location_id => $location_name) {
$form['widgets'][$location_id][$widget_id]['enable'] = [
'#type' => 'checkbox',
'#title' => $widget_info['label'],
'#default_value' => $this->options['widgets'][$location_id][$widget_id]['enable'],
'#description' => $this
->t('Should @name be rendered at the @location of the slides.', [
'@name' => $widget_info['label'],
'@location' => $location_name,
]),
'#dependency' => [
'edit-style-options-slideshow-type' => $compatible_slideshows,
],
];
$form['widgets'][$location_id][$widget_id]['wrapper'] = [
'#markup' => '<div class="vs-dependent">',
];
$weight = isset($this->options['widgets'][$location_id][$widget_id]['weight']) ? $this->options['widgets'][$location_id][$widget_id]['weight'] : 0;
if ($weight > count($widgets)) {
$weight = count($widgets);
}
$form['widgets'][$location_id][$widget_id]['weight'] = [
'#type' => 'select',
'#title' => $this
->t('Weight of the @name', [
'@name' => $widget_info['label'],
]),
'#default_value' => $weight,
'#options' => $weights,
'#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'],
]),
'#prefix' => '<div class="vs-dependent">',
'#suffix' => '</div>',
'#states' => [
'visible' => [
':input[name="style_options[widgets][' . $location_id . '][' . $widget_id . '][enable]"]' => [
'checked' => TRUE,
],
],
],
];
$widget_dependency = 'style_options[widgets][' . $location_id . '][' . $widget_id . ']';
$configuration = [];
if (!empty($this->options['widgets'][$location_id][$widget_id])) {
$configuration = $this->options['widgets'][$location_id][$widget_id];
}
$configuration['dependency'] = $widget_dependency;
$instance = $widgetTypeManager
->createInstance($widget_id, $configuration);
$form['widgets'][$location_id][$widget_id] = $instance
->buildConfigurationForm($form['widgets'][$location_id][$widget_id], $form_state);
$form['widgets'][$location_id][$widget_id]['wrapper_close'] = [
'#markup' => '</div>',
];
}
}
}
}
foreach ($location as $location_id => $location_name) {
if (count(array_keys($form['widgets'][$location_id])) == 1) {
unset($form['widgets'][$location_id]);
}
}
if (empty($form['widgets'])) {
unset($form['widgets']);
unset($form['widgets_header']);
}
$form['views_slideshow_wrapper_close'] = [
'#markup' => '</div>',
];
$form['#attached']['library'] = [
'views_slideshow/form',
];
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
foreach ($typeManager
->getDefinitions() as $id => $definition) {
$type = $typeManager
->createInstance($id);
$type
->validateConfigurationForm($form, $form_state);
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$typeManager = \Drupal::service('plugin.manager.views_slideshow.slideshow_type');
foreach ($typeManager
->getDefinitions() as $id => $definition) {
$type = $typeManager
->createInstance($id);
$type
->submitConfigurationForm($form, $form_state);
}
}
}