diff --git a/opencase.module b/opencase.module index 136b7a0..bc2ae36 100644 --- a/opencase.module +++ b/opencase.module @@ -7,6 +7,33 @@ use Drupal\Core\Routing\RouteMatchInterface; + +/** + * Implements hook_block_access + * + * Forbids the opencase_contextual_menu block on pages where it has no content. + * (Without this, it was displaying an empty sidebar) + */ +function opencase_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) { + if ($operation == 'view' && $block->getPluginId() == 'opencase_contextual_menu') { + $route_name = \Drupal::routeMatch()->getRouteName(); + $routes_where_it_should_be_shown = [ + 'entity.oc_actor.canonical', + 'view.cases.page_1', + 'entity.oc_case.canonical', + 'view.activities.page_1', + 'entity.oc_activity.canonical' + ]; + error_log(!in_array($route_name, $routes_where_it_should_be_shown)); + return \Drupal\Core\Access\AccessResult::forbiddenIf(!in_array($route_name, $routes_where_it_should_be_shown)) + ->addCacheableDependency($block); + } + + // No opinion. + return \Drupal\Core\Access\AccessResult::neutral(); +} + + /** * Implements hook_page_attachments * diff --git a/src/Plugin/Block/ContextualMenu.php b/src/Plugin/Block/ContextualMenu.php index 0047d88..d7dad03 100644 --- a/src/Plugin/Block/ContextualMenu.php +++ b/src/Plugin/Block/ContextualMenu.php @@ -10,8 +10,11 @@ use Drupal\opencase\Utils; /** * Provides a 'ContextualMenu' block. * + * Displays contextual links on certain pages. + * The block is forbidden by hook_block_access on other pages, so if more are added they need adding there too. + * * @Block( - * id = "contextual_menu", + * id = "opencase_contextual_menu", * admin_label = @Translation("OpenCase Contextual Menu"), * ) */