views_plugin_exposed_form_input_required.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_exposed_form_input_required.
  5. */
  6. /**
  7. * Exposed form plugin that provides an exposed form with required input.
  8. *
  9. * @ingroup views_exposed_form_plugins
  10. */
  11. class views_plugin_exposed_form_input_required extends views_plugin_exposed_form {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['text_input_required'] = array('default' => 'Select any filter and click on Apply to see results', 'translatable' => TRUE);
  18. $options['text_input_required_format'] = array('default' => NULL);
  19. return $options;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. $form['text_input_required'] = array(
  27. '#type' => 'text_format',
  28. '#title' => t('Text on demand'),
  29. '#description' => t('Text to display instead of results until the user selects and applies an exposed filter.'),
  30. '#default_value' => $this->options['text_input_required'],
  31. '#format' => isset($this->options['text_input_required_format']) ? $this->options['text_input_required_format'] : filter_default_format(),
  32. '#wysiwyg' => FALSE,
  33. );
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function options_submit(&$form, &$form_state) {
  39. $form_state['values']['exposed_form_options']['text_input_required_format'] = $form_state['values']['exposed_form_options']['text_input_required']['format'];
  40. $form_state['values']['exposed_form_options']['text_input_required'] = $form_state['values']['exposed_form_options']['text_input_required']['value'];
  41. parent::options_submit($form, $form_state);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function exposed_filter_applied() {
  47. static $cache = NULL;
  48. if (!isset($cache)) {
  49. $view = $this->view;
  50. if (is_array($view->filter) && count($view->filter)) {
  51. foreach ($view->filter as $filter_id => $filter) {
  52. if ($filter->is_exposed()) {
  53. $identifier = $filter->options['expose']['identifier'];
  54. if (isset($view->exposed_input[$identifier])) {
  55. $cache = TRUE;
  56. return $cache;
  57. }
  58. }
  59. }
  60. }
  61. $cache = FALSE;
  62. }
  63. return $cache;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function pre_render($values) {
  69. if (!$this->exposed_filter_applied()) {
  70. $options = array(
  71. 'id' => 'area',
  72. 'table' => 'views',
  73. 'field' => 'area',
  74. 'label' => '',
  75. 'relationship' => 'none',
  76. 'group_type' => 'group',
  77. 'content' => $this->options['text_input_required'],
  78. 'format' => $this->options['text_input_required_format'],
  79. 'empty' => TRUE,
  80. );
  81. $handler = views_get_handler('views', 'area', 'area');
  82. $handler->init($this->view, $options);
  83. $this->display->handler->handlers['empty'] = array(
  84. 'area' => $handler,
  85. );
  86. $this->display->handler->set_option('empty', array('text' => $options));
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function query() {
  93. if (!$this->exposed_filter_applied()) {
  94. // We return with no query; this will force the empty text.
  95. $this->view->built = TRUE;
  96. $this->view->executed = TRUE;
  97. $this->view->result = array();
  98. }
  99. else {
  100. parent::query();
  101. }
  102. }
  103. }