DrupalSliderThumbnails.php in Drupal Slider 8
File
src/Plugin/views/field/DrupalSliderThumbnails.php
View source
<?php
namespace Drupal\drupal_slider\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class DrupalSliderThumbnails extends FieldPluginBase {
public function query() {
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['thumbnails'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['relationship']['#access'] = FALSE;
$form['thumbnails'] = [
'#type' => 'textarea',
'#title' => $this
->t('Thumbnails image url'),
'#description' => $this
->t("Give the Thumbnail image URL token"),
'#default_value' => $this->options['thumbnails'],
];
$form['replacements'] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $this
->t('Replacement Variables'),
];
$views_fields = $this->view->display_handler
->getHandlers('field');
foreach ($views_fields as $field => $handler) {
if ($field == $this->options['id']) {
break;
}
$items[] = "{{ {$field} }}";
}
$form['replacements']['variables'] = [
'#theme' => 'item_list',
'#items' => $items,
];
parent::buildOptionsForm($form, $form_state);
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
}
public function cleanVar($var) {
$unparsed = isset($var->last_render) ? $var->last_render : '';
return trim($unparsed);
}
public function render(ResultRow $values) {
if (!empty($values)) {
$thumbnails = $this->options['thumbnails'];
$fields = $this->view->display_handler
->getHandlers('field');
$labels = $this->view->display_handler
->getFieldLabels();
foreach ($labels as $key => $var) {
if (strpos($thumbnails, "{{ {$key} }}") !== FALSE) {
$field = $this
->cleanVar($fields[$key]);
$thumbnails = str_replace("{{ {$key} }}", $field, $thumbnails);
}
}
return $thumbnails;
}
}
}