Skip to content

Commit a947aaf

Browse files
[8.x] Add sort direction option for HasManyFieldtype (#795)
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
1 parent 0e41999 commit a947aaf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Fieldtypes/HasManyFieldtype.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ protected function configFieldItems(): array
3737
'reorderable' => true,
3838
],
3939
],
40+
'order_direction' => [
41+
'display' => __('Order Direction'),
42+
'instructions' => __('Which direction should the items be sorted in?'),
43+
'type' => 'select',
44+
'width' => 50,
45+
'default' => 'asc',
46+
'options' => [
47+
'asc' => __('Ascending'),
48+
'desc' => __('Descending'),
49+
],
50+
'if' => [
51+
'reorderable' => true,
52+
],
53+
],
4054
];
4155

4256
return array_merge(parent::configFieldItems(), $config);

src/Http/Controllers/CP/Traits/PreparesModels.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ protected function prepareModelForPublishForm(Resource $resource, Model $model):
8282
// When re-ordering is enabled, ensure the models are returned in the correct order.
8383
if ($field->get('reorderable', false)) {
8484
$orderColumn = $field->get('order_column');
85+
$orderDirection = $field->get('order_direction', 'asc');
8586
$relationshipName = $resource->eloquentRelationships()->get($field->handle());
8687

8788
$value = $model->{$relationshipName}()
88-
->reorder($orderColumn, 'ASC')
89+
->reorder($orderColumn, $orderDirection)
8990
->get();
9091
}
9192
}

0 commit comments

Comments
 (0)