Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Plugin/Usage/FileUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct(

// This regex now handles spaces in filenames.
$this->uriRegex = '/(?<!"preview_image":{"id":")((' . implode('|', array_keys($this->wrappers)) . '):\/\/(.*?)\.(.*?))([\s|:"*?<>|\\\\]|$)/m';
$this->mediaReferenceRegex = '/\[media-reference:file:(.*?)\]/m';
$this->mediaReferenceRegex = '#(?:<img[^>]*data-entity-uuid=\\\"([^"]+)\\\"[^>]*src=\\\"([^"]+)\\\"|\[media-reference:file:([^\]]+)\])#i';
$this->fileRepository = $fileRepository;
}

Expand Down Expand Up @@ -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')
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down