I have a view which has 5 menu tabs. I want to hide the tab if it doesn't have any results. Can anyone please help me with the suggestions.


2 Answers 2


Can be done with Jquery Check for the class "view-empty", if this div has inner div, then that means it has some content, if not hide that parent tab div.


By implementing hooks as below

function custom_tabs_hide_menu_local_tasks_alter(&$data, $route_name, $route_match) {

// Check if the current route matches the route where your Views is being displayed.

if ($route_name === 'entity.view.your_view_id.page_1') {

// Replace 'your_view_id' with the actual ID of your view.
// Check if the View has no results.

$view = \Drupal\views\Views::getView('your_view_id'); 

// Replace 'your_view_id' with the actual ID of your view.

$view->setDisplay('page_1'); // Replace 'page_1' with the display ID of your view.


if (empty($view->result)) {
  // Hide the tab.

} }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.