View source
<?php
namespace Drupal\views_test_data\Plugin\views\display;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
class DisplayTest extends DisplayPluginBase {
protected $usesAttachments = TRUE;
public function getType() {
return 'test';
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['test_option'] = [
'default' => '',
];
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['display_test'] = [
'title' => $this
->t('Display test settings'),
'column' => 'second',
'build' => [
'#weight' => -100,
],
];
$test_option = $this
->getOption('test_option') ?: $this
->t('Empty');
$options['test_option'] = [
'category' => 'display_test',
'title' => $this
->t('Test option'),
'value' => views_ui_truncate($test_option, 24),
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'test_option':
$form['#title'] .= $this
->t('Test option');
$form['test_option'] = [
'#title' => $this
->t('Test option'),
'#type' => 'textfield',
'#description' => $this
->t('This is a textfield for test_option.'),
'#default_value' => $this
->getOption('test_option'),
];
break;
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
parent::validateOptionsForm($form, $form_state);
\Drupal::logger('views')
->notice($form_state
->getValue('test_option'));
switch ($form_state
->get('section')) {
case 'test_option':
if (!trim($form_state
->getValue('test_option'))) {
$form_state
->setError($form['test_option'], $this
->t('You cannot have an empty option.'));
}
break;
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'test_option':
$this
->setOption('test_option', $form_state
->getValue('test_option'));
break;
}
}
public function execute() {
$this->view
->build();
$render = $this->view
->render();
$render['#prefix'] = '<h1>' . Xss::filterAdmin($this->options['test_option']) . '</h1>';
return $render;
}
public function preview() {
return $this
->execute();
}
public function calculateDependencies() {
return parent::calculateDependencies() + [
'content' => [
'DisplayTest',
],
];
}
public function validate() {
$errors = parent::validate();
foreach ($this->view->displayHandlers as $display_handler) {
$errors[] = 'error';
}
return $errors;
}
}