3 namespace Drupal\views\Plugin\views\exposed_form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Views;
9 * Exposed form plugin that provides an exposed form with required input.
11 * @ingroup views_exposed_form_plugins
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.")
19 class InputRequired extends ExposedFormPluginBase {
21 protected function defineOptions() {
22 $options = parent::defineOptions();
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];
29 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
30 parent::buildOptionsForm($form, $form_state);
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(),
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);
49 protected function exposedFilterApplied() {
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])) {
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
74 if (!$this->exposedFilterApplied()) {
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.
87 // @see \Drupal\views\Plugin\views\area\Text::render()
88 'value' => $this->options['text_input_required'],
89 'format' => $this->options['text_input_required_format'],
92 $handler = Views::handlerManager('area')->getHandler($options);
93 $handler->init($this->view, $this->displayHandler, $options);
94 $this->displayHandler->handlers['empty'] = [
97 // Override the existing empty result message (if applicable).
98 $this->displayHandler->setOption('empty', ['text' => $options]);
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 = [];