diff --git a/handlers/views_handler_area.inc b/handlers/views_handler_area.inc index 295e14d..99802ad 100644 --- a/handlers/views_handler_area.inc +++ b/handlers/views_handler_area.inc @@ -75,6 +75,18 @@ class views_handler_area extends views_handler { } /** + * Is the area empty. + * + * 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 + */ + 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 b8fad3f..7ff687e 100644 --- a/handlers/views_handler_area_view.inc +++ b/handlers/views_handler_area_view.inc @@ -11,6 +11,13 @@ */ class views_handler_area_view extends views_handler_area { + /** + * Stores whether the embedded view is actually empty. + * + * @var bool + */ + var $is_empty; + function option_definition() { $options = parent::option_definition(); @@ -70,13 +77,25 @@ class views_handler_area_view extends views_handler_area { } else { if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) { - return $view->preview($display_id, $this->view->args); + $output = $view->preview($display_id, $this->view->args); } else { - return $view->preview($display_id); + $output = $view->preview($display_id); } + $this->is_empty = $view->display_handler->output_is_empty(); + return $output; } } return ''; } + + function is_empty() { + if (isset($this->is_empty)) { + return $this->is_empty; + } + else { + return parent::is_empty(); + } + } + } diff --git a/includes/view.inc b/includes/view.inc index bba6f51..0649951 100644 --- a/includes/view.inc +++ b/includes/view.inc @@ -653,6 +653,7 @@ class view extends views_db_object { foreach ($handlers as $id => $handler) { $handlers[$id]->post_execute($this->result); } + dvm($this->display_handler->output_is_empty()); } } @@ -1209,6 +1210,17 @@ class view extends views_db_object { } /** + * Helper method to detect whether a view result is totally empty. + * + * @return bool + */ + function output_is_empty() { + if (!empty($this->display_handler)) { + return $this->display_handler->output_is_empty(); + } + } + + /** * Run attachments and let the display do what it needs to do prior * to running. */ diff --git a/plugins/views_plugin_display.inc b/plugins/views_plugin_display.inc index 8c3adf4..6e6134c 100644 --- a/plugins/views_plugin_display.inc +++ b/plugins/views_plugin_display.inc @@ -2510,6 +2510,30 @@ class views_plugin_display extends views_plugin { } /** + * Does the view shows anything. + * + * If a view has no result and neither the empty, nor the footer nor the header + * does show anything return FALSE. + * + * @return bool + */ + function output_is_empty() { + if (!empty($this->view->result)) { + return FALSE; + } + $empty = TRUE; + // Check whether any of the area handlers aren't empty. + $area_types = array('empty', 'footer', 'header'); + foreach ($area_types as $type) { + $handlers = $this->get_handlers($type); + foreach ($handlers as $handler) { + $empty &= $handler->is_empty(); + } + } + return $empty; + } + + /** * If this display creates a block, implement one of these. */ function hook_block_list($delta = 0, $edit = array()) { return array(); } diff --git a/plugins/views_plugin_display_block.inc b/plugins/views_plugin_display_block.inc index 4338d75..9f8f719 100644 --- a/plugins/views_plugin_display_block.inc +++ b/plugins/views_plugin_display_block.inc @@ -15,6 +15,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; } @@ -52,7 +53,10 @@ class views_plugin_display_block extends views_plugin_display { // display, and arguments should be set on the view. $info['content'] = $this->view->render(); $info['subject'] = filter_xss_admin($this->view->get_title()); - if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) { + if (!$this->output_is_empty()) { + + } + if ((!$this->get_option('block_hide_empty') && !$this->output_is_empty()) || !empty($this->view->style_plugin->definition['even empty'])) { return $info; } } @@ -96,6 +100,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('Hide block if the view output is empty'), + 'value' => !$this->get_option('block_hide_empty') ? t('Hide') : t('Show'), + ); } /** @@ -142,6 +152,14 @@ class views_plugin_display_block extends views_plugin_display { '#default_value' => $this->get_option('block_description'), ); break; + case 'block_hide_empty': + $form['#title'] .= t('Block empty settings'); + $form['block_hide_empty'] = array( + '#title' => t('Hide block if no result/empty text'), + '#description' => t('Hide 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 'block_caching': $form['#title'] .= t('Block caching type'); @@ -166,8 +184,9 @@ class views_plugin_display_block extends views_plugin_display { case 'display_id': $this->update_block_bid($form_state['view']->name, $this->display->id, $this->display->new_id); break; + case 'block_hide_empty': case 'block_description': - $this->set_option('block_description', $form_state['values']['block_description']); + $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]); break; case 'block_caching': $this->set_option('block_caching', $form_state['values']['block_caching']);