Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / modules / views_test_data / src / Plugin / views / display / DisplayTest.php
1 <?php
2
3 namespace Drupal\views_test_data\Plugin\views\display;
4
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8
9 /**
10  * Defines a Display test plugin.
11  *
12  * @ViewsDisplay(
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"}
19  * )
20  */
21 class DisplayTest extends DisplayPluginBase {
22
23   /**
24    * Whether the display allows attachments.
25    *
26    * @var bool
27    */
28   protected $usesAttachments = TRUE;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getType() {
34     return 'test';
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function defineOptions() {
41     $options = parent::defineOptions();
42     $options['test_option'] = ['default' => ''];
43
44     return $options;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function optionsSummary(&$categories, &$options) {
51     parent::optionsSummary($categories, $options);
52
53     $categories['display_test'] = [
54       'title' => $this->t('Display test settings'),
55       'column' => 'second',
56       'build' => [
57         '#weight' => -100,
58       ],
59     ];
60
61     $test_option = $this->getOption('test_option') ?: $this->t('Empty');
62
63     $options['test_option'] = [
64       'category' => 'display_test',
65       'title' => $this->t('Test option'),
66       'value' => views_ui_truncate($test_option, 24),
67     ];
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
74     parent::buildOptionsForm($form, $form_state);
75
76     switch ($form_state->get('section')) {
77       case 'test_option':
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'),
84         ];
85         break;
86     }
87   }
88
89   /**
90    * {@inheritdoc}
91    */
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')) {
96       case 'test_option':
97         if (!trim($form_state->getValue('test_option'))) {
98           $form_state->setError($form['test_option'], $this->t('You cannot have an empty option.'));
99         }
100         break;
101     }
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
108     parent::submitOptionsForm($form, $form_state);
109     switch ($form_state->get('section')) {
110       case 'test_option':
111         $this->setOption('test_option', $form_state->getValue('test_option'));
112         break;
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function execute() {
120     $this->view->build();
121
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>';
125
126     return $render;
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function preview() {
133     return $this->execute();
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function calculateDependencies() {
140     return parent::calculateDependencies() + [
141       'content' => ['DisplayTest'],
142     ];
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function validate() {
149     $errors = parent::validate();
150     foreach ($this->view->displayHandlers as $display_handler) {
151       $errors[] = 'error';
152     }
153     return $errors;
154   }
155
156 }