diff --git a/core/core.services.yml b/core/core.services.yml index 7d56112..3a91b9a 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -316,6 +316,10 @@ services: tags: - { name: paramconverter } arguments: ['@entity.manager'] + route_subscriber.callback: + class: Drupal\Core\EventSubscriber\CallbackRouteSubscriber + tags: + - { name: event_subscriber } route_subscriber.entity: class: Drupal\Core\EventSubscriber\EntityRouteAlterSubscriber tags: diff --git a/core/lib/Drupal/Core/EventSubscriber/CallbackRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/CallbackRouteSubscriber.php new file mode 100644 index 0000000..3207033 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/CallbackRouteSubscriber.php @@ -0,0 +1,47 @@ +getRouteCollection(); + foreach ($collection as $name => $route) { + if ($route->hasOption('_callback')) { + foreach ((array) $route->getOption('_callback') as $callback) { + if (!call_user_func($callback)) { + $collection->remove($name); + } + } + } + } + } + +} \ No newline at end of file diff --git a/core/modules/dblog/dblog.routing.yml b/core/modules/dblog/dblog.routing.yml index 31f1b1c..938433b 100644 --- a/core/modules/dblog/dblog.routing.yml +++ b/core/modules/dblog/dblog.routing.yml @@ -27,3 +27,13 @@ dblog.access_denied: _content: '\Drupal\dblog\Controller\DbLogController::accessDenied' requirements: _permission: 'access site reports' + +dblog.search: + path: '/admin/reports/search' + defaults: + _content: '\Drupal\dblog\Controller\DbLogController::search' + _title: 'Top search phrases' + options: + _callback: ['\Drupal\dblog\Controller\DbLogController::searchExists'] + requirements: + _permission: 'access site reports' diff --git a/core/modules/dblog/dblog.services.yml b/core/modules/dblog/dblog.services.yml deleted file mode 100644 index 815734c..0000000 --- a/core/modules/dblog/dblog.services.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - dblog.route_subscriber: - class: Drupal\dblog\Routing\RouteSubscriber - arguments: ['@module_handler'] - tags: - - { name: event_subscriber} diff --git a/core/modules/dblog/lib/Drupal/dblog/Controller/DbLogController.php b/core/modules/dblog/lib/Drupal/dblog/Controller/DbLogController.php index 795f689..60e3a6e 100644 --- a/core/modules/dblog/lib/Drupal/dblog/Controller/DbLogController.php +++ b/core/modules/dblog/lib/Drupal/dblog/Controller/DbLogController.php @@ -343,4 +343,11 @@ public function search() { return dblog_top('search'); } + /** + * Route callback to determine if route should be added. + */ + public static function searchExists() { + return module_exists('search'); + } + } diff --git a/core/modules/dblog/lib/Drupal/dblog/Routing/RouteSubscriber.php b/core/modules/dblog/lib/Drupal/dblog/Routing/RouteSubscriber.php deleted file mode 100644 index f04492e..0000000 --- a/core/modules/dblog/lib/Drupal/dblog/Routing/RouteSubscriber.php +++ /dev/null @@ -1,73 +0,0 @@ -moduleHandler = $module_handler; - } - - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() { - $events[RoutingEvents::DYNAMIC] = 'routes'; - return $events; - } - - /** - * Generate dynamic routes for various dblog pages. - * - * @param \Drupal\Core\Routing\RouteBuildEvent $event - * The route building event. - * - * @return \Symfony\Component\Routing\RouteCollection - * The route collection that contains the new dynamic route. - */ - public function routes(RouteBuildEvent $event) { - $collection = $event->getRouteCollection(); - if ($this->moduleHandler->moduleExists('search')) { - // The block entity listing page. - $route = new Route( - 'admin/reports/search', - array( - '_content' => '\Drupal\dblog\Controller\DbLogController::search', - '_title' => 'Top search phrases', - ), - array( - '_permission' => 'access site reports', - ) - ); - $collection->add('dblog.search', $route); - } - } - -}