Pull merge.
[yaffs-website] / web / core / modules / views / src / Plugin / views / exposed_form / InputRequired.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\exposed_form;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Views;
7
8 /**
9  * Exposed form plugin that provides an exposed form with required input.
10  *
11  * @ingroup views_exposed_form_plugins
12  *
13  * @ViewsExposedForm(
14  *   id = "input_required",
15  *   title = @Translation("Input required"),
16  *   help = @Translation("An exposed form that only renders a view if the form contains user input.")
17  * )
18  */
19 class InputRequired extends ExposedFormPluginBase {
20
21   protected function defineOptions() {
22     $options = parent::defineOptions();
23
24     $options['text_input_required'] = ['default' => $this->t('Select any filter and click on Apply to see results')];
25     $options['text_input_required_format'] = ['default' => NULL];
26     return $options;
27   }
28
29   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
30     parent::buildOptionsForm($form, $form_state);
31
32     $form['text_input_required'] = [
33       '#type' => 'text_format',
34       '#title' => $this->t('Text on demand'),
35       '#description' => $this->t('Text to display instead of results until the user selects and applies an exposed filter.'),
36       '#default_value' => $this->options['text_input_required'],
37       '#format' => isset($this->options['text_input_required_format']) ? $this->options['text_input_required_format'] : filter_default_format(),
38       '#editor' => FALSE,
39     ];
40   }
41
42   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
43     $exposed_form_options = $form_state->getValue('exposed_form_options');
44     $form_state->setValue(['exposed_form_options', 'text_input_required_format'], $exposed_form_options['text_input_required']['format']);
45     $form_state->setValue(['exposed_form_options', 'text_input_required'], $exposed_form_options['text_input_required']['value']);
46     parent::submitOptionsForm($form, $form_state);
47   }
48
49   protected function exposedFilterApplied() {
50     static $cache = NULL;
51     if (!isset($cache)) {
52       $view = $this->view;
53       if (is_array($view->filter) && count($view->filter)) {
54         foreach ($view->filter as $filter) {
55           if ($filter->isExposed()) {
56             $identifier = $filter->options['expose']['identifier'];
57             if (isset($view->getExposedInput()[$identifier])) {
58               $cache = TRUE;
59               return $cache;
60             }
61           }
62         }
63       }
64       $cache = FALSE;
65     }
66
67     return $cache;
68   }
69
70   public function preRender($values) {
71     // Display the "text on demand" if needed. This is a site builder-defined
72     // text to display instead of results until the user selects and applies
73     // an exposed filter.
74     if (!$this->exposedFilterApplied()) {
75       $options = [
76         'id' => 'area',
77         'table' => 'views',
78         'field' => 'area',
79         'label' => '',
80         'relationship' => 'none',
81         'group_type' => 'group',
82         // We need to set the "Display even if view has no result" option to
83         // TRUE as the input required exposed form plugin will always force an
84         // empty result if no exposed filters are applied.
85         'empty' => TRUE,
86         'content' => [
87           // @see \Drupal\views\Plugin\views\area\Text::render()
88           'value' => $this->options['text_input_required'],
89           'format' => $this->options['text_input_required_format'],
90         ],
91       ];
92       $handler = Views::handlerManager('area')->getHandler($options);
93       $handler->init($this->view, $this->displayHandler, $options);
94       $this->displayHandler->handlers['empty'] = [
95         'area' => $handler,
96       ];
97       // Override the existing empty result message (if applicable).
98       $this->displayHandler->setOption('empty', ['text' => $options]);
99     }
100   }
101
102   public function query() {
103     if (!$this->exposedFilterApplied()) {
104       // We return with no query; this will force the empty text.
105       $this->view->built = TRUE;
106       $this->view->executed = TRUE;
107       $this->view->result = [];
108     }
109     else {
110       parent::query();
111     }
112   }
113
114 }