diff --git a/code/GraphQL/Resolvers/FolderTypeResolver.php b/code/GraphQL/Resolvers/FolderTypeResolver.php index 16216a220..fade7cf45 100644 --- a/code/GraphQL/Resolvers/FolderTypeResolver.php +++ b/code/GraphQL/Resolvers/FolderTypeResolver.php @@ -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 { @@ -155,7 +167,7 @@ public static function sortChildren(array $context): Closure $field, File::class ); - $sort[$normalised] = $dir; + $sort[$normalised] = $direction; } $list = $list->sort($sort);