diff --git a/handlers/views_handler_area.inc b/handlers/views_handler_area.inc index 9fed11c..9826f5a 100644 --- a/handlers/views_handler_area.inc +++ b/handlers/views_handler_area.inc @@ -95,6 +95,19 @@ class views_handler_area extends views_handler { } /** + * Does that area have nothing to show. + * + * This method should be overridden by more complex handlers where the output + * is not static and maybe itself be empty if it's rendered. + * + * @return bool + * Return TRUE if the area is empty, else FALSE. + */ + public function is_empty() { + return empty($this->options['empty']); + } + + /** * Area handlers shouldn't have groupby. */ function use_group_by() { diff --git a/handlers/views_handler_area_view.inc b/handlers/views_handler_area_view.inc index 3b72bf6..f3b2f1b 100644 --- a/handlers/views_handler_area_view.inc +++ b/handlers/views_handler_area_view.inc @@ -12,6 +12,13 @@ */ class views_handler_area_view extends views_handler_area { + /** + * Stores whether the embedded view is actually empty. + * + * @var bool + */ + protected $is_empty; + function option_definition() { $options = parent::option_definition(); @@ -53,15 +60,26 @@ class views_handler_area_view extends views_handler_area { function render($empty = FALSE) { if ($view = $this->loadView()) { if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) { - return $view->preview(NULL, $this->view->args); + $output = $view->preview(NULL, $this->view->args); } else { - return $view->preview(NULL); + $output = $view->preview(NULL); } + $this->is_empty = $view->display_handler->output_is_empty(); + return $output; } return ''; } + public function is_empty() { + if (isset($this->is_empty)) { + return $this->is_empty; + } + else { + return parent::is_empty(); + } + } + /** * Loads the used view for rendering. * diff --git a/plugins/views_plugin_display.inc b/plugins/views_plugin_display.inc index b7aa6ce..14044d4 100644 --- a/plugins/views_plugin_display.inc +++ b/plugins/views_plugin_display.inc @@ -2814,6 +2814,34 @@ class views_plugin_display extends views_plugin { } /** + * Is the output of the view empty. + * + * If a view has no result and neither the empty, nor the footer nor the header + * does show anything return FALSE. + * + * @return bool + * Returns TRUE if the output is empty, else FALSE. + */ + public function output_is_empty() { + if (!empty($this->view->result)) { + return FALSE; + } + + // Check whether all of the area handlers are empty. + foreach (array('empty', 'footer', 'header') as $type) { + $handlers = $this->get_handlers($type); + foreach ($handlers as $handler) { + // If one is not empty, return FALSE now. + if (!$handler->is_empty()) { + return FALSE; + } + } + } + + return TRUE; + } + + /** * Provide the block system with any exposed widget blocks for this display. */ function get_special_blocks() { diff --git a/plugins/views_plugin_display_block.inc b/plugins/views_plugin_display_block.inc index 88d22d0..7adf304 100644 --- a/plugins/views_plugin_display_block.inc +++ b/plugins/views_plugin_display_block.inc @@ -16,6 +16,7 @@ class views_plugin_display_block extends views_plugin_display { $options['block_description'] = array('default' => '', 'translatable' => TRUE); $options['block_caching'] = array('default' => DRUPAL_NO_CACHE); + $options['block_hide_empty'] = array('default' => FALSE); return $options; } @@ -54,7 +55,10 @@ class views_plugin_display_block extends views_plugin_display { $info['content'] = $this->view->render(); $title = $this->view->get_title(); $info['subject'] = ($title == '') ? '' : filter_xss_admin($title); - if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) { + if ($this->output_is_empty() && !$this->get_option('block_hide_empty') && empty($this->view->style_plugin->definition['even empty'])) { + return array(); + } + else { return $info; } } @@ -93,6 +97,12 @@ class views_plugin_display_block extends views_plugin_display { 'title' => t('Block caching'), 'value' => $types[$this->get_cache_type()], ); + + $options['block_hide_empty'] = array( + 'category' => 'other', + 'title' => t('Show block if the view output is empty'), + 'value' => $this->get_option('block_hide_empty') ? t('Show') : t('Hide'), + ); } /** @@ -148,6 +158,16 @@ class views_plugin_display_block extends views_plugin_display { '#default_value' => $this->get_cache_type(), ); break; + case 'block_hide_empty': + $form['#title'] .= t('Block empty settings'); + + $form['block_hide_empty'] = array( + '#title' => t('Show block if no result/empty text'), + '#type' => 'checkbox', + '#description' => t('Show the block if there is no result and no empty text and no header/footer which is shown on empty result'), + '#default_value' => $this->get_option('block_hide_empty'), + ); + break; case 'exposed_form_options': $this->view->init_handlers(); if (!$this->uses_exposed() && parent::uses_exposed()) { @@ -177,6 +197,9 @@ class views_plugin_display_block extends views_plugin_display { $this->set_option('block_caching', $form_state['values']['block_caching']); $this->save_block_cache($form_state['view']->name . '-'. $form_state['display_id'], $form_state['values']['block_caching']); break; + case 'block_hide_empty': + $this->set_option('block_hide_empty', $form_state['values']['block_hide_empty']); + break; } }