From 2300b13b66a34f7e443b3c3dbae7f7cbfb618403 Mon Sep 17 00:00:00 2001 From: Noel Date: Thu, 16 Apr 2026 11:03:10 +0800 Subject: [PATCH 1/2] Fix file usages not working for s3 files. --- src/Plugin/Usage/FileUsage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Usage/FileUsage.php b/src/Plugin/Usage/FileUsage.php index 640407ab..7b21e04c 100644 --- a/src/Plugin/Usage/FileUsage.php +++ b/src/Plugin/Usage/FileUsage.php @@ -112,7 +112,7 @@ public function __construct( // This regex now handles spaces in filenames. $this->uriRegex = '/(?wrappers)) . '):\/\/(.*?)\.(.*?))([\s|:"*?<>|\\\\]|$)/m'; - $this->mediaReferenceRegex = '/\[media-reference:file:(.*?)\]/m'; + $this->mediaReferenceRegex = '#(?:]*data-entity-uuid=\\\"([^"]+)\\\"[^>]*src=\\\"([^"]+)\\\"|\[media-reference:file:([^\]]+)\])#i'; $this->fileRepository = $fileRepository; } From 1fb4e7cae1421f649945e6f5517657077c166456 Mon Sep 17 00:00:00 2001 From: Noel Date: Fri, 17 Apr 2026 09:12:51 +0800 Subject: [PATCH 2/2] Fix file usage on scenario where all files are remove on a page. --- src/Plugin/Usage/FileUsage.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Plugin/Usage/FileUsage.php b/src/Plugin/Usage/FileUsage.php index 7b21e04c..ffc36bbe 100644 --- a/src/Plugin/Usage/FileUsage.php +++ b/src/Plugin/Usage/FileUsage.php @@ -187,6 +187,16 @@ private function getFileEntityByUuid($uuid) { private function registerCoreFileUsage($files, EntityInterface $entity) { if ($entity->id() && strlen($entity->id()) < 32) { + + // Load previously referenced file IDs so we can invalidate their cache. + $existingFids = $this->connection->select('file_usage', 'fu') + ->fields('fu', ['fid']) + ->condition('module', 'cohesion') + ->condition('type', $entity->getEntityTypeId()) + ->condition('id', $entity->id()) + ->execute() + ->fetchCol(); + // Remove 'cohesion' registered usage of all files for scanned entity. try { $this->connection->delete('file_usage') @@ -199,6 +209,14 @@ private function registerCoreFileUsage($files, EntityInterface $entity) { return; } + // Invalidate cache for files that were previously referenced. + if (!empty($existingFids)) { + $existingFiles = $this->storage->loadMultiple($existingFids); + foreach ($existingFiles as $existingFile) { + $this->refreshFileListCache($existingFile); + } + } + // Add file usages back in one by one, so we end up with the correct count // for duplicates. foreach ($files as $file) { @@ -319,10 +337,7 @@ public function scanForInstancesOfThisType($data, EntityInterface $entity) { } } - // Register file usages with core 'file.usage'. - if (!empty($files)) { - $this->registerCoreFileUsage($files, $entity); - } + $this->registerCoreFileUsage($files, $entity); return $entities; }