views_plugin_exposed_form_input_required.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['text_input_required'] = array('default' => 'Select any filter and click on Apply to see results', 'translatable' => TRUE);
  15. $options['text_input_required_format'] = array('default' => NULL);
  16. return $options;
  17. }
  18. function options_form(&$form, &$form_state) {
  19. parent::options_form($form, $form_state);
  20. $form['text_input_required'] = array(
  21. '#type' => 'text_format',
  22. '#title' => t('Text on demand'),
  23. '#description' => t('Text to display instead of results until the user selects and applies an exposed filter.'),
  24. '#default_value' => $this->options['text_input_required'],
  25. '#format' => isset($this->options['text_input_required_format']) ? $this->options['text_input_required_format'] : filter_default_format(),
  26. '#wysiwyg' => FALSE,
  27. );
  28. }
  29. function options_submit(&$form, &$form_state) {
  30. $form_state['values']['exposed_form_options']['text_input_required_format'] = $form_state['values']['exposed_form_options']['text_input_required']['format'];
  31. $form_state['values']['exposed_form_options']['text_input_required'] = $form_state['values']['exposed_form_options']['text_input_required']['value'];
  32. parent::options_submit($form, $form_state);
  33. }
  34. function exposed_filter_applied() {
  35. static $cache = NULL;
  36. if (!isset($cache)) {
  37. $view = $this->view;
  38. if (is_array($view->filter) && count($view->filter)) {
  39. foreach ($view->filter as $filter_id => $filter) {
  40. if ($filter->is_exposed()) {
  41. $identifier = $filter->options['expose']['identifier'];
  42. if (isset($view->exposed_input[$identifier])) {
  43. $cache = TRUE;
  44. return $cache;
  45. }
  46. }
  47. }
  48. }
  49. $cache = FALSE;
  50. }
  51. return $cache;
  52. }
  53. function pre_render($values) {
  54. if (!$this->exposed_filter_applied()) {
  55. $options = array(
  56. 'id' => 'area',
  57. 'table' => 'views',
  58. 'field' => 'area',
  59. 'label' => '',
  60. 'relationship' => 'none',
  61. 'group_type' => 'group',
  62. 'content' => $this->options['text_input_required'],
  63. 'format' => $this->options['text_input_required_format'],
  64. 'empty' => TRUE,
  65. );
  66. $handler = views_get_handler('views', 'area', 'area');
  67. $handler->init($this->view, $options);
  68. $this->display->handler->handlers['empty'] = array(
  69. 'area' => $handler,
  70. );
  71. $this->display->handler->set_option('empty', array('text' => $options));
  72. }
  73. }
  74. function query() {
  75. if (!$this->exposed_filter_applied()) {
  76. // We return with no query; this will force the empty text.
  77. $this->view->built = TRUE;
  78. $this->view->executed = TRUE;
  79. $this->view->result = array();
  80. }
  81. else {
  82. parent::query();
  83. }
  84. }
  85. }