Rebuild the Drupal route cache.
authorMikey O'Toole <mikey@mjco.uk>
Thu, 25 Mar 2021 19:32:51 +0000 (19:32 +0000)
committerMikey O'Toole <mikey@mjco.uk>
Thu, 25 Mar 2021 19:32:51 +0000 (19:32 +0000)
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
CRM/Utils/System/Base.php
CRM/Utils/System/Drupal8.php

index bc537d6b269f9c01d2bc94c318f21d1c6cdabcfc..6611dc00d556a8a10365631caaeb701bfb301037 100644 (file)
@@ -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();
index e126a6d5f329d84041f9ba70f62d5118721b031f..6a1ce83351b07dd05a77bbb46d52c70d82f1e487 100644 (file)
@@ -1012,4 +1012,10 @@ abstract class CRM_Utils_System_Base {
     return [];
   }
 
+  /**
+   * Invalidates the cache of dynamic routes and forces a rebuild.
+   */
+  public function invalidateRouteCache() {
+  }
+
 }
index a7bb1aaede5fa4912ca0eafab88f8cc644eb5903..a506c36b9392cd1e375bade2118292d2d245fcf2 100644 (file)
@@ -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();
+    }
+  }
+
 }