Skip to content

Commit

Permalink
FIX Include default_sort in sortChildren method
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 25, 2022
1 parent 3605a40 commit 5023adb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions code/GraphQL/Resolvers/FolderTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,20 @@ public static function sortChildren(array $context): Closure
$classNameField = "(CASE WHEN \"ClassName\"={$className} THEN 1 ELSE 0 END)";
$sortArgs = array_merge([$classNameField => 'DESC'], $sortArgs);

// Partial support for File::$default_sort
// File::$default_sort = '"Name"' - which is the default, is supported here
// Does not support "Table"."Field" syntax or arrays
$defaultSort = File::config()->get('default_sort');
if (is_string($defaultSort) &&
preg_match('#^"?([a-zA-Z0-9_]+)"?( asc| desc| ASC| DESC|)$#', $defaultSort, $matches)
) {
$field = $matches[1];
$direction = strtoupper($matches[2]) ?: 'ASC';
$sortArgs = array_merge($sortArgs, [$field => $direction]);
}

$sort = [];
foreach ($sortArgs as $field => $dir) {
foreach ($sortArgs as $field => $direction) {
if ($field == $classNameField) {
$normalised = $classNameField;
} else {
Expand All @@ -155,7 +167,7 @@ public static function sortChildren(array $context): Closure
$field,
File::class
);
$sort[$normalised] = $dir;
$sort[$normalised] = $direction;
}
$list = $list->sort($sort);

Expand Down

0 comments on commit 5023adb

Please sign in to comment.