Skip to content

Commit 14039a7

Browse files
committed
feat(taxonomies): add sorting support for taxonomies with sort_by and sort_dir fields
1 parent 638b54d commit 14039a7

4 files changed

Lines changed: 86 additions & 4 deletions

File tree

src/Stache/Stores/TaxonomiesStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function makeItemFromFile($path, $contents)
4444
->title(Arr::get($data, 'title'))
4545
->cascade(Arr::get($data, 'inject', []))
4646
->searchIndex(Arr::get($data, 'search_index'))
47+
->sortField(Arr::get($data, 'sort_by'))
48+
->sortDirection(Arr::get($data, 'sort_dir'))
4749
->defaultPublishState($this->getDefaultPublishState($data))
4850
->sites($sites)
4951
->previewTargets($this->normalizePreviewTargets(Arr::get($data, 'preview_targets', [])))

src/Taxonomies/Taxonomy.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Statamic\Facades\Site;
2929
use Statamic\Facades\Stache;
3030
use Statamic\Facades\URL;
31+
use Statamic\Support\Arr;
3132
use Statamic\Support\Str;
3233
use Statamic\Support\Traits\FluentlyGetsAndSets;
3334

@@ -44,6 +45,8 @@ class Taxonomy implements Arrayable, ArrayAccess, AugmentableContract, ContainsQ
4445
protected $collection;
4546
protected $defaultPublishState = true;
4647
protected $searchIndex;
48+
protected $sortField;
49+
protected $sortDirection;
4750
protected $previewTargets = [];
4851
protected $template;
4952
protected $termTemplate;
@@ -177,14 +180,32 @@ public function hasVisibleTermBlueprint()
177180
return $this->termBlueprints()->reject->hidden()->isNotEmpty();
178181
}
179182

180-
public function sortField()
183+
public function sortField($field = null)
181184
{
182-
return 'title'; // todo
185+
return $this
186+
->fluentlyGetOrSet('sortField')
187+
->getter(function ($sortField) {
188+
return $sortField ?? 'title';
189+
})
190+
->args(func_get_args());
183191
}
184192

185-
public function sortDirection()
193+
public function sortDirection($dir = null)
186194
{
187-
return 'asc'; // todo
195+
return $this
196+
->fluentlyGetOrSet('sortDirection')
197+
->getter(function ($sortDirection) {
198+
if ($sortDirection) {
199+
return $sortDirection;
200+
}
201+
202+
if ($this->sortField) {
203+
return 'asc';
204+
}
205+
206+
return 'asc';
207+
})
208+
->args(func_get_args());
188209
}
189210

190211
public function queryTerms()
@@ -290,6 +311,11 @@ public function fileData()
290311
'layout' => $this->layout,
291312
];
292313

314+
$data = Arr::removeNullValues(array_merge($data, [
315+
'sort_by' => $this->sortField,
316+
'sort_dir' => $this->sortDirection,
317+
]));
318+
293319
if (Site::multiEnabled()) {
294320
$data['sites'] = $this->sites;
295321
}

tests/Data/Taxonomies/TaxonomyTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ public function it_gets_the_url_with_a_collection()
182182
$this->assertEquals('http://localhost/blog/tags', $taxonomy->absoluteUrl());
183183
}
184184

185+
#[Test]
186+
public function it_gets_sort_field_and_direction()
187+
{
188+
$taxonomy = new Taxonomy;
189+
$this->assertEquals('title', $taxonomy->sortField());
190+
$this->assertEquals('asc', $taxonomy->sortDirection());
191+
192+
$taxonomy->sortField('foo');
193+
$this->assertEquals('foo', $taxonomy->sortField());
194+
$this->assertEquals('asc', $taxonomy->sortDirection());
195+
196+
$taxonomy->sortDirection('desc');
197+
$this->assertEquals('desc', $taxonomy->sortDirection());
198+
}
199+
185200
#[Test]
186201
public function it_gets_evaluated_augmented_value_using_magic_property()
187202
{
@@ -308,6 +323,30 @@ public function it_get_terms_count_from_multi_sites()
308323
$this->assertEquals(3, $taxonomy->queryTerms()->pluck('slug')->unique()->count());
309324
}
310325

326+
#[Test]
327+
public function it_has_file_data()
328+
{
329+
$taxonomy = (new Taxonomy)
330+
->handle('tags')
331+
->title('Tags');
332+
333+
$this->assertEquals([
334+
'title' => 'Tags',
335+
'inject' => [],
336+
], $taxonomy->fileData());
337+
338+
$taxonomy
339+
->sortField('foo')
340+
->sortDirection('desc');
341+
342+
$this->assertEquals([
343+
'title' => 'Tags',
344+
'sort_by' => 'foo',
345+
'sort_dir' => 'desc',
346+
'inject' => [],
347+
], $taxonomy->fileData());
348+
}
349+
311350
#[Test]
312351
public function it_saves_through_the_api()
313352
{

tests/Stache/Stores/TaxonomiesStoreTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ public function it_makes_taxonomy_instances_from_files()
6767
$this->assertEquals('Example', $item->title());
6868
}
6969

70+
#[Test]
71+
public function it_hydrates_sort_field_and_direction()
72+
{
73+
$contents = <<<'YAML'
74+
title: Tags
75+
sort_by: foo
76+
sort_dir: desc
77+
YAML;
78+
79+
$item = $this->store->makeItemFromFile($this->tempDir.'/tags.yaml', $contents);
80+
81+
$this->assertEquals('foo', $item->sortField());
82+
$this->assertEquals('desc', $item->sortDirection());
83+
}
84+
7085
#[Test]
7186
public function it_normalizes_preview_target_url_into_format()
7287
{

0 commit comments

Comments
 (0)