From 43f09f8b9487892055b3b5dcdc4de060ca87df55 Mon Sep 17 00:00:00 2001
From: Coleman Watts <coleman@civicrm.org>
Date: Mon, 18 Jul 2022 11:26:47 -0400
Subject: [PATCH] APIv4 - Flush navigation caches after writing or deleting
 navigation item

Historically the expectation was that this cache gets flushed manually.
I've kept that unchanged for legacy code (v3 create/delete and direct BAO::add)
but with APIv4 and onward the cache will get flushed automatically after a write operation.
---
 CRM/Core/BAO/Navigation.php | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/CRM/Core/BAO/Navigation.php b/CRM/Core/BAO/Navigation.php
index 688d399d6f5a..f3e72a6b3fdd 100644
--- a/CRM/Core/BAO/Navigation.php
+++ b/CRM/Core/BAO/Navigation.php
@@ -19,6 +19,42 @@ class CRM_Core_BAO_Navigation extends CRM_Core_DAO_Navigation {
   // Number of characters in the menu js cache key
   const CACHE_KEY_STRLEN = 8;
 
+  /**
+   * Override parent method to flush caches after a write op.
+   *
+   * Note: this only applies to APIv4 because v3 uses the singular writeRecord.
+   *
+   * @param array[] $records
+   * @return CRM_Core_DAO_Navigation[]
+   * @throws CRM_Core_Exception
+   */
+  public static function writeRecords($records): array {
+    $results = [];
+    foreach ($records as $record) {
+      $results[] = self::writeRecord($record);
+    }
+    self::resetNavigation();
+    return $results;
+  }
+
+  /**
+   * Override parent method to flush caches after delete.
+   *
+   * Note: this only applies to APIv4 because v3 uses the singular writeRecord.
+   *
+   * @param array[] $records
+   * @return CRM_Core_DAO_Navigation[]
+   * @throws CRM_Core_Exception
+   */
+  public static function deleteRecords(array $records) {
+    $results = [];
+    foreach ($records as $record) {
+      $results[] = self::deleteRecord($record);
+    }
+    self::resetNavigation();
+    return $results;
+  }
+
   /**
    * Update the is_active flag in the db.
    *