From a11c108436c61696bd8dea5f27930fa3f85bcc44 Mon Sep 17 00:00:00 2001 From: Mikey O'Toole Date: Thu, 25 Mar 2021 19:32:51 +0000 Subject: [PATCH] Rebuild the Drupal route cache. This is linked to [issue 158](https://lab.civicrm.org/dev/drupal/-/issues/158) and [issue 37](https://lab.civicrm.org/dev/drupal/-/issues/37). It takes an alternative approach, adding a Util hook function to rebuild the route cache which we can then invoke from multiple places. Trigger `rebuildDrupalRouteCache()` when running `triggerRebuild()` Fix checkstyle issues Change function from `protected` to `public` Update Drupal8.php Add base for `invalidateRouteCache()` Add invalidateRouteCache function Add call to userFramework->invalidateRouteCache Update Base.php Correct `userFramework` to `userSystem` Remove stray whitespace. Move invalidateRouteCache call to the same place as resetNavigation. Update Invoke.php Switch to the `router.builder' option as invalidating the cache doesn't cut it. --- CRM/Core/Invoke.php | 1 + CRM/Utils/System/Base.php | 6 ++++++ CRM/Utils/System/Drupal8.php | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/CRM/Core/Invoke.php b/CRM/Core/Invoke.php index bc537d6b26..6611dc00d5 100644 --- a/CRM/Core/Invoke.php +++ b/CRM/Core/Invoke.php @@ -394,6 +394,7 @@ class CRM_Core_Invoke { CRM_Utils_Request::retrieve('triggerRebuild', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET') ) { CRM_Core_DAO::triggerRebuild(); + $config->userSystem->invalidateRouteCache(); } CRM_Core_DAO_AllCoreTables::reinitializeCache(TRUE); CRM_Core_ManagedEntities::singleton(TRUE)->reconcile(); diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index e126a6d5f3..6a1ce83351 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1012,4 +1012,10 @@ abstract class CRM_Utils_System_Base { return []; } + /** + * Invalidates the cache of dynamic routes and forces a rebuild. + */ + public function invalidateRouteCache() { + } + } diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index a7bb1aaede..a506c36b93 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -858,4 +858,14 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return \Drupal::entityTypeManager()->getStorage('user')->load($userID); } + /** + * Helper function to rebuild the Drupal 8 or 9 dynamic routing cache. + * We need to do this after enabling extensions that add routes and it's worth doing when we reset Civi paths. + */ + public function invalidateRouteCache() { + if (class_exists('\Drupal') && \Drupal::hasContainer()) { + \Drupal::service('router.builder')->rebuild(); + } + } + } -- 2.25.1