Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX folders always go first when ordering #936

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion code/GraphQL/FolderTypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,52 @@ public function resolveChildrenConnection(
$filter['parentId'] = $object->ID;
$list = $filterInputType->filterList($list, $filter);

if (!isset($args['sortBy'])) {
// only show folders first if no manual ordering is set

$list = $list->alterDataQuery(static function (DataQuery $dataQuery) {
$query = $dataQuery->query();
$existingOrderBys = [];
foreach ($query->getOrderBy() as $field => $direction) {
if (strpos($field, '.') === false) {
// some fields may be surrogates added by extending augmentSQL (e.g. fluent)
// we have to preserve those expressions rather than auto-generated names
// that SQLSelect::addOrderBy leaves for them (usually that's alike _SortColumn0)
//
// see related issues for more details:
// - https://github.com/silverstripe/silverstripe-asset-admin/issues/820
// - https://github.com/silverstripe/silverstripe-asset-admin/issues/893
$field = $query->expressionForField(trim($field, '"')) ?: $field;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of these issues is closed and the other is the one that is resolved by this PR. I'm going to remove these links from the comments.

}

$existingOrderBys[$field] = $direction;
}

// Folders should always go first due to backwards compatibility
// See https://github.com/silverstripe/silverstripe-asset-admin/issues/893
$dataQuery->sort(
sprintf(
'(CASE WHEN "ClassName"=%s THEN 1 ELSE 0 END)',
DB::get_conn()->quoteString(Folder::class)
),
'DESC',
true
);

foreach ($existingOrderBys as $field => $dir) {
$dataQuery->sort($field, $dir, false);
}

return $dataQuery;
});
}

// Filter by permission
$ids = $list->column('ID');
// DataQuery::column ignores surrogate sorting fields
// see https://github.com/silverstripe/silverstripe-framework/issues/8926
// the following line is a workaround for `$ids = $list->column('ID');`
$ids = $list->dataQuery()->execute()->column('ID');

$permissionChecker = File::singleton()->getPermissionChecker();
$canViewIDs = array_keys(array_filter($permissionChecker->canViewMultiple(
$ids,
Expand Down
20 changes: 20 additions & 0 deletions tests/php/GraphQL/FolderTypeCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ class FolderTypeCreatorTest extends SapphireTest

protected $usesDatabase = true;

public function testItSortsChildrenOnTypeByDefault()
{
$rootFolder = Folder::singleton();
$file = File::create(['Name' => 'aaa file']);
$file->write();
$folder = Folder::create(['Name' => 'bbb folder']);
$folder->write();
$list = $this->resolveChildrenConnection(
$rootFolder,
[]
);
$this->assertEquals(
[
$folder->Name,
$file->Name,
],
$list['edges']->column('Name')
);
}

public function testItDoesNotFilterByParentIdWithRecursiveFlag()
{
$rootFolder = Folder::singleton();
Expand Down