-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from reachweb/sort-filter-values
Add sort capability
- Loading branch information
Showing
6 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicLivewireFilters\Exceptions; | ||
|
||
use Exception; | ||
|
||
class FieldOptionsCannotFindTaxonomyField extends Exception | ||
{ | ||
public function __construct($sortyBy, $handle) | ||
{ | ||
$message = "Cannot find field [{$sortyBy}] in the taxonomy [{$handle}] in order to sort the filter options."; | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicLivewireFilters\Exceptions; | ||
|
||
use Exception; | ||
|
||
class FieldOptionsCannotSortException extends Exception | ||
{ | ||
public function __construct($sortyBy, $handle) | ||
{ | ||
$message = "Cannot sort field [{$handle}] by [{$sortyBy}]. Maybe you have mixed up the field type?"; | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicLivewireFilters\Http\Livewire\Traits; | ||
|
||
use Livewire\Attributes\Locked; | ||
use Reach\StatamicLivewireFilters\Exceptions\FieldOptionsCannotFindTaxonomyField; | ||
use Reach\StatamicLivewireFilters\Exceptions\FieldOptionsCannotSortException; | ||
use Statamic\Facades\Taxonomy; | ||
|
||
trait IsSortable | ||
{ | ||
#[Locked] | ||
public $sort; | ||
|
||
public function mountIsSortable(): void | ||
{ | ||
if ($this->sort === null) { | ||
return; | ||
} | ||
|
||
[$sortBy, $sortDirection] = explode(':', $this->sort); | ||
$fieldType = $this->statamic_field['type']; | ||
$fieldHandle = $this->statamic_field['handle']; | ||
|
||
switch ($sortBy) { | ||
case 'key': | ||
case 'label': | ||
if ($fieldType !== 'checkboxes' && $fieldType !== 'radio' && $fieldType !== 'select') { | ||
throw new FieldOptionsCannotSortException($sortBy, $fieldHandle); | ||
} | ||
if ($sortBy === 'key') { | ||
$this->sortOptionsByKey($sortDirection); | ||
} else { | ||
$this->sortOptionsByValue($sortDirection); | ||
} | ||
break; | ||
case 'slug': | ||
case 'title': | ||
if ($fieldType !== 'terms') { | ||
throw new FieldOptionsCannotSortException($sortBy, $fieldHandle); | ||
} | ||
if ($sortBy === 'slug') { | ||
$this->sortOptionsByKey($sortDirection); | ||
} else { | ||
$this->sortOptionsByValue($sortDirection); | ||
} | ||
break; | ||
default: | ||
$this->sortCustomField($sortBy, $sortDirection); | ||
} | ||
} | ||
|
||
protected function sortOptionsByKey($sortDirection): void | ||
{ | ||
$options = collect($this->statamic_field['options']); | ||
|
||
if ($sortDirection === 'asc') { | ||
$options = $options->sortKeys(); | ||
} else { | ||
$options = $options->sortKeysDesc(); | ||
} | ||
|
||
$this->statamic_field['options'] = $options->all(); | ||
} | ||
|
||
protected function sortOptionsByValue($sortDirection): void | ||
{ | ||
$options = collect($this->statamic_field['options']); | ||
|
||
if ($sortDirection === 'asc') { | ||
$options = $options->sort(); | ||
} else { | ||
$options = $options->sortDesc(); | ||
} | ||
|
||
$this->statamic_field['options'] = $options->all(); | ||
} | ||
|
||
// If the sortyBy is not one of the default ones, we need to search the terms of the field | ||
protected function sortCustomField($sortBy, $sortDirection): void | ||
{ | ||
$fieldType = $this->statamic_field['type']; | ||
$fieldHandle = $this->statamic_field['handle']; | ||
|
||
// If it's not a terms field, it can't have custom fields. | ||
if ($fieldType !== 'terms') { | ||
throw new FieldOptionsCannotSortException($sortBy, $fieldHandle); | ||
} | ||
|
||
$terms = $this->getTaxonomyTermsSortedBy($fieldHandle, $sortBy, $sortDirection); | ||
} | ||
|
||
protected function getTaxonomyTermsSortedBy($handle, $sortBy, $sortDirection): void | ||
{ | ||
$taxonomy = Taxonomy::findByHandle($handle); | ||
|
||
// Check if the field exists | ||
if (! $taxonomy->termBlueprint()->fields()->all()->has($sortBy)) { | ||
throw new FieldOptionsCannotFindTaxonomyField($sortBy, $handle); | ||
} | ||
|
||
$this->statamic_field['options'] = $taxonomy->queryTerms()->orderBy($sortBy, $sortDirection)->get()->flatMap(function ($term) { | ||
return [ | ||
$term->slug() => $term->title(), | ||
]; | ||
})->all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters