1

I have a custom module with the following function:

function custom_hide_product_range_views_post_execute(ViewExecutable $view) {

  if ($view->id() == 'brand_product_range') {
    if (count($view->result) < 2) {
        $view->display_handler->setOption('css_class', 'hide');
    }
  }
}

This hook_views_post_execute is removing the view if it has less than two results.

There is another block that depends on this block, so I cannot just remove the whole block I have to hide it using CSS


1- What I would like to do:

The problem is The view is hidden but the block (parent of view) still exists so the title of the block still exists, How can I get the object of the block from the $view variable, so I can hide it as well?


2- Failed approach!

I tried to go the other way around by using hook_block_view_alter to get the block then get the view object from the block object, this approach failed because (I think) the block is constructed first, the view inside the block was yet to be constructed and it contained no results at all.

2
  • There is an option in views to hide the block if no results are present.
    – Kevin
    Jan 10 '18 at 15:23
  • yes, I need to hide the block if it had less than 2 results, not if it has no results. Jan 10 '18 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.