Skip to content

Commit

Permalink
[5.x] Allow sorting folders in asset browser (#10935)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Oct 18, 2024
1 parent 467e09e commit 3a7d6fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Assets/AssetFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public function lastModified()
return $date;
}

public function size()
{
$size = 0;

foreach ($this->assets() as $asset) {
$size += $asset->size();
}

return $size;
}

public function save()
{
$this->disk()->put($this->path().'/.gitkeep', '');
Expand Down
19 changes: 17 additions & 2 deletions src/Http/Controllers/CP/Assets/BrowserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Pagination\Paginator;
use Statamic\Assets\AssetFolder;
use Statamic\Contracts\Assets\AssetContainer as AssetContainerContract;
use Statamic\Exceptions\AuthorizationException;
use Statamic\Facades\Asset;
Expand Down Expand Up @@ -90,10 +91,24 @@ public function folder(Request $request, $container, $path = '/')
if ($page >= $lastFolderPage) {
$query = $folder->queryAssets();

if ($request->sort) {
if ($sort = $request->sort) {
$sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy';

$folders = $folders->$sortByMethod(
fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename()
);

$query->orderBy($request->sort, $request->order ?? 'asc');
} else {
$query->orderBy($container->sortField(), $container->sortDirection());
$sort = $container->sortField();
$order = $container->sortDirection();
$sortByMethod = $request->order === 'desc' ? 'sortByDesc' : 'sortBy';

$folders = $folders->$sortByMethod(
fn (AssetFolder $folder) => method_exists($folder, $sort) ? $folder->$sort() : $folder->basename()
);

$query->orderBy($sort, $order);
}

$this->applyQueryScopes($query, $request->all());
Expand Down

0 comments on commit 3a7d6fc

Please sign in to comment.