diff --git a/typo3/sysext/filelist/Classes/FileList.php b/typo3/sysext/filelist/Classes/FileList.php index 6b8fb6d..470aad8 100644 --- a/typo3/sysext/filelist/Classes/FileList.php +++ b/typo3/sysext/filelist/Classes/FileList.php @@ -384,7 +384,54 @@ class FileList extends AbstractRecordList $folders = $storage->getFoldersInFolder($this->folderObject, $foldersFrom, $foldersNum, true, false, trim($this->sort), (bool)$this->sortRev); $files = $this->folderObject->getFiles($filesFrom, $filesNum, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, false, trim($this->sort), (bool)$this->sortRev); - $this->totalItems = $foldersCount + $filesCount; + + // HF-MOD: START - Ref sorting + + if (trim($this->sort) === '_REF_') { + $filesWithRefs = []; + + foreach ($files as $name => $file) { + $databaseConnection = $this->getDatabaseConnection(); + $table = 'sys_refindex'; + $referenceCount = $databaseConnection->exec_SELECTcountRows( + '*', + $table, + 'ref_table=' . $databaseConnection->fullQuoteStr('sys_file', $table) + . ' AND ref_uid=' . (int)$file->getUid() + . ' AND deleted=0' + . ' AND tablename != ' . $databaseConnection->fullQuoteStr('sys_file_metadata', $table) + ); + + $filesWithRefs[$name] = [ + 'refs' => $referenceCount, + 'file' => $file, + ]; + } + + uasort($filesWithRefs, function ($a, $b) { + if ($a['refs'] == $b['refs']) { + return 0; + } + + if ((bool) $this->sortRev) { + return ($a['refs'] < $b['refs']) ? 1 : -1; + } + + return ($a['refs'] < $b['refs']) ? -1 : 1; + }); + + $files = []; + + foreach ($filesWithRefs as $name => $file) { + $files[$name] = $file['file']; + } + + } + + // HF-MOD: END - Ref sorting + + + $this->totalItems = $foldersCount + $filesCount; // Adds the code of files/dirs $out = ''; $titleCol = 'file';