View source
<?php
namespace Drupal\flexslider_views\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
class FlexSlider extends StylePluginBase {
protected $usesRowPlugin = TRUE;
protected $usesFields = TRUE;
protected $usesOptions = TRUE;
public function evenEmpty() {
return FALSE;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['optionset'] = [
'default' => 'default',
];
$options['captionfield'] = [
'default' => '',
];
$options['id'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['flexslider'] = [
'#type' => 'fieldset',
'#title' => $this
->t('FlexSlider'),
];
$form['flexslider']['optionset'] = [
'#title' => $this
->t('Option set'),
'#type' => 'select',
'#options' => flexslider_optionset_list(),
'#default_value' => $this->options['optionset'],
];
$captionfield_options = [
'' => $this
->t('None'),
];
foreach ($this->displayHandler
->getHandlers('field') as $field => $handler) {
$captionfield_options[$field] = $handler
->adminLabel();
}
$form['flexslider']['captionfield'] = [
'#type' => 'select',
'#title' => $this
->t('Caption Field'),
'#description' => $this
->t("Select a field to be used as the caption. This can also be set manually by adding the '.flex-caption' class to a field. Required to use thumbnail captions."),
'#options' => $captionfield_options,
'#default_value' => $this->options['captionfield'],
];
$form['flexslider']['id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Element ID'),
'#description' => $this
->t("Manually define the FlexSlider container ID attribute <em>Ensure you don't display similar ID elements on the same page</em>."),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => $this->options['id'],
];
}
public function render() {
$sets = parent::render();
$output = $sets;
foreach ($sets as $key => &$set) {
if (!empty($this->options['captionfield'])) {
$caption_field = $this->options['captionfield'];
foreach ($set['#rows'] as $index => $row) {
$set['#rows'][$index]['#caption'] = $this->rendered_fields[$index][$caption_field];
}
}
$output[$key] = [
'#theme' => $this
->themeFunctions(),
'#view' => $this->view,
'#options' => $this->options,
'#rows' => $set['#rows'],
'#title' => $set['#title'],
];
}
return $output;
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$flexslider_options = $form_state
->getValue([
'style_options',
'flexslider',
]);
foreach ($flexslider_options as $key => $value) {
$form_state
->setValue([
'style_options',
$key,
], $value);
}
$form_state
->setValue([
'style_options',
'flexslider',
], NULL);
}
}