diff --git a/src/Plugin/Usage/FileUsage.php b/src/Plugin/Usage/FileUsage.php index 640407ab..ffc36bbe 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; } @@ -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; }