I would like my local tasks (tabs), defined in mymodule.links.task.yml, to appear only if a certain module is enabled. If a certain module is disabled, I want the tab to be hidden.
mymodule.alpha:
route_name: mymodule.alpha
base_route: mymodule.alpha
title: 'Alpha'
mymodule.bravo:
route_name: mymodule.bravo
base_route: mymodule.alpha
title: 'Bravo'
My understanding is that this can be acheived by adding a _module_dependencies key to my routing.yml file.
_module_dependencies: Optionally use this key to specify one or more modules that are required to serve this path. You can combine module names with a + (plus) for an AND relationship or , (comma) for an OR relationship. For example, _module_dependencies: 'node + search' means both node and search are needed, _module_dependencies: 'node, search' means either node or search are needed. If your module already depends on other modules for its behavior (via info.yml dependencies), there is, of course, no need for specifying the dependency here as well; however, for optional dependencies, where routes are provided only if those optional dependent modules are also enabled, this is a useful option.
https://www.drupal.org/docs/8/api/routing-system/structure-of-routes
mymodule.alpha:
path: '/admin/reports/mymodule/alpha'
defaults:
_controller: Drupal\mymodule\Controller\AlphaController::render
_title: 'Alpha'
requirements:
_permission: 'access site reports'
_module_dependencies: 'comment' # this is module is enabled, this line does not cause a white screen on cache rebuild
mymodule.bravo:
path: '/admin/reports/mymodule/bravo'
defaults:
_controller: Drupal\mymodule\Controller\BravoController::render
_title: 'Bravo'
requirements:
_permission: 'access site reports'
_module_dependencies: 'statistics' # this is module is disabled, this line does cause a white screen on cache rebuild
However, if a module is disabled, I get this error:
Symfony\Component\Routing\Exception\RouteNotFoundException: Route "mymodule.bravo" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 187 of /var/www/mysite/core/lib/Drupal/Core/Routing/RouteProvider.php).
The error is thrown even when I visit: /admin/reports/mymodule/alpha.
_custom_access:
and do the module check logic in here...$config['system.logging']['error_level'] = 'verbose';
to your development environment settings.php file.