3 namespace Drupal\views_test_data\Plugin\views\display;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
10 * Defines a Display test plugin.
13 * id = "display_test",
14 * title = @Translation("Display test"),
15 * help = @Translation("Defines a display test plugin."),
16 * theme = "views_view",
17 * register_theme = FALSE,
18 * contextual_links_locations = {"view"}
21 class DisplayTest extends DisplayPluginBase {
24 * Whether the display allows attachments.
28 protected $usesAttachments = TRUE;
33 public function getType() {
40 protected function defineOptions() {
41 $options = parent::defineOptions();
42 $options['test_option'] = ['default' => ''];
50 public function optionsSummary(&$categories, &$options) {
51 parent::optionsSummary($categories, $options);
53 $categories['display_test'] = [
54 'title' => $this->t('Display test settings'),
61 $test_option = $this->getOption('test_option') ?: $this->t('Empty');
63 $options['test_option'] = [
64 'category' => 'display_test',
65 'title' => $this->t('Test option'),
66 'value' => views_ui_truncate($test_option, 24),
73 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
74 parent::buildOptionsForm($form, $form_state);
76 switch ($form_state->get('section')) {
78 $form['#title'] .= $this->t('Test option');
79 $form['test_option'] = [
80 '#title' => $this->t('Test option'),
81 '#type' => 'textfield',
82 '#description' => $this->t('This is a textfield for test_option.'),
83 '#default_value' => $this->getOption('test_option'),
92 public function validateOptionsForm(&$form, FormStateInterface $form_state) {
93 parent::validateOptionsForm($form, $form_state);
94 \Drupal::logger('views')->notice($form_state->getValue('test_option'));
95 switch ($form_state->get('section')) {
97 if (!trim($form_state->getValue('test_option'))) {
98 $form_state->setError($form['test_option'], $this->t('You cannot have an empty option.'));
107 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
108 parent::submitOptionsForm($form, $form_state);
109 switch ($form_state->get('section')) {
111 $this->setOption('test_option', $form_state->getValue('test_option'));
119 public function execute() {
120 $this->view->build();
122 $render = $this->view->render();
123 // Render the test option as the title before the view output.
124 $render['#prefix'] = '<h1>' . Xss::filterAdmin($this->options['test_option']) . '</h1>';
132 public function preview() {
133 return $this->execute();
139 public function calculateDependencies() {
140 return parent::calculateDependencies() + [
141 'content' => ['DisplayTest'],
148 public function validate() {
149 $errors = parent::validate();
150 foreach ($this->view->displayHandlers as $display_handler) {