InputRequired.php 3.44 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\exposed_form\InputRequired.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\exposed_form;
9

10
use Drupal\Component\Annotation\Plugin;
11
use Drupal\Core\Annotation\Translation;
12
use Drupal\views\Views;
13

merlinofchaos's avatar
merlinofchaos committed
14 15 16 17
/**
 * Exposed form plugin that provides an exposed form with required input.
 *
 * @ingroup views_exposed_form_plugins
18
 *
19
 * @Plugin(
aspilicious's avatar
aspilicious committed
20
 *   id = "input_required",
21
 *   title = @Translation("Input required"),
22
 *   help = @Translation("An exposed form that only renders a view if the form contains user input.")
23 24
 * )
 */
25
class InputRequired extends ExposedFormPluginBase {
merlinofchaos's avatar
merlinofchaos committed
26

27 28
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
29

30
    $options['text_input_required'] = array('default' => 'Select any filter and click on Apply to see results', 'translatable' => TRUE);
merlinofchaos's avatar
merlinofchaos committed
31 32 33 34
    $options['text_input_required_format'] = array('default' => NULL);
    return $options;
  }

35 36
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
37 38 39 40 41 42 43 44 45 46 47

    $form['text_input_required'] = array(
      '#type' => 'text_format',
      '#title' => t('Text on demand'),
      '#description' => t('Text to display instead of results until the user selects and applies an exposed filter.'),
      '#default_value' => $this->options['text_input_required'],
      '#format' => isset($this->options['text_input_required_format']) ? $this->options['text_input_required_format'] : filter_default_format(),
      '#wysiwyg' => FALSE,
    );
  }

48
  public function submitOptionsForm(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
49 50
    $form_state['values']['exposed_form_options']['text_input_required_format'] = $form_state['values']['exposed_form_options']['text_input_required']['format'];
    $form_state['values']['exposed_form_options']['text_input_required'] = $form_state['values']['exposed_form_options']['text_input_required']['value'];
51
    parent::submitOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
52 53
  }

54
  protected function exposedFilterApplied() {
merlinofchaos's avatar
merlinofchaos committed
55 56 57 58 59
    static $cache = NULL;
    if (!isset($cache)) {
      $view = $this->view;
      if (is_array($view->filter) && count($view->filter)) {
        foreach ($view->filter as $filter_id => $filter) {
60
          if ($filter->isExposed()) {
merlinofchaos's avatar
merlinofchaos committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74
            $identifier = $filter->options['expose']['identifier'];
            if (isset($view->exposed_input[$identifier])) {
              $cache = TRUE;
              return $cache;
            }
          }
        }
      }
      $cache = FALSE;
    }

    return $cache;
  }

75
  public function preRender($values) {
76
    if (!$this->exposedFilterApplied()) {
merlinofchaos's avatar
merlinofchaos committed
77 78 79 80 81 82 83 84 85 86
      $options = array(
        'id' => 'area',
        'table' => 'views',
        'field' => 'area',
        'label' => '',
        'relationship' => 'none',
        'group_type' => 'group',
        'content' => $this->options['text_input_required'],
        'format' => $this->options['text_input_required_format'],
      );
87
      $handler = Views::handlerManager('area')->getHandler($options);
merlinofchaos's avatar
merlinofchaos committed
88
      $handler->init($this->view, $options);
89
      $this->displayHandler->handlers['empty'] = array(
merlinofchaos's avatar
merlinofchaos committed
90 91
        'area' => $handler,
      );
92
      $this->displayHandler->setOption('empty', array('text' => $options));
merlinofchaos's avatar
merlinofchaos committed
93 94 95
    }
  }

96
  public function query() {
97
    if (!$this->exposedFilterApplied()) {
merlinofchaos's avatar
merlinofchaos committed
98 99 100 101 102 103 104 105 106 107 108
      // We return with no query; this will force the empty text.
      $this->view->built = TRUE;
      $this->view->executed = TRUE;
      $this->view->result = array();
    }
    else {
      parent::query();
    }
  }

}