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 efd5620
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions code/GraphQL/Resolvers/FolderTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ 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];
$dir = strtoupper($matches[2]) ?: 'ASC';
if (!array_key_exists($field, $sortArgs)) {
// add default sort to the end of the array so that it has lowest priority
$sortArgs = array_merge($sortArgs, [$field => $dir]);
}
}

$sort = [];
foreach ($sortArgs as $field => $dir) {
if ($field == $classNameField) {
Expand Down

0 comments on commit efd5620

Please sign in to comment.