From fe173ff4d9479fe1ee030eab3c3f53273b2cb0d9 Mon Sep 17 00:00:00 2001 From: Philipp Daun Date: Thu, 21 Nov 2024 16:25:33 +0100 Subject: [PATCH] [5.x] Enable asset folder sort across pages (#11130) --- .../CP/Assets/BrowserController.php | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/Http/Controllers/CP/Assets/BrowserController.php b/src/Http/Controllers/CP/Assets/BrowserController.php index 2f1c058f26..f394ff7385 100644 --- a/src/Http/Controllers/CP/Assets/BrowserController.php +++ b/src/Http/Controllers/CP/Assets/BrowserController.php @@ -82,35 +82,30 @@ public function folder(Request $request, $container, $path = '/') $folders = $folder->assetFolders(); $totalFolders = $folders->count(); - $folders = $folders->slice(($page - 1) * $perPage, $perPage); $lastFolderPage = (int) ceil($totalFolders / $perPage) ?: 1; $totalAssets = $folder->queryAssets()->count(); $totalItems = $totalAssets + $totalFolders; - if ($page >= $lastFolderPage) { - $query = $folder->queryAssets(); - - if ($sort = $request->sort) { - $sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy'; - - $folders = $folders->$sortByMethod( - fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename() - ); + if ($request->sort) { + $sort = $request->sort; + $order = $request->order ?? 'asc'; + } else { + $sort = $container->sortField(); + $order = $container->sortDirection(); + } - $query->orderBy($request->sort, $request->order ?? 'asc'); - } else { - $sort = $container->sortField(); - $order = $container->sortDirection(); - $sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy'; + $sortByMethod = $order === 'desc' ? 'sortByDesc' : 'sortBy'; - $folders = $folders->$sortByMethod( - fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename() - ); + $folders = $folders->$sortByMethod( + fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename() + ); - $query->orderBy($sort, $order); - } + $folders = $folders->slice(($page - 1) * $perPage, $perPage); + if ($page >= $lastFolderPage) { + $query = $folder->queryAssets(); + $query->orderBy($sort, $order); $this->applyQueryScopes($query, $request->all()); $offset = $page === $lastFolderPage