Skip to content

Commit

Permalink
Merge pull request #12 from caseydwyer/feature-currency-filter
Browse files Browse the repository at this point in the history
[1.x] Feature: List view filtering for Currency fieldtype
  • Loading branch information
doefom authored Apr 11, 2024
2 parents 58fd2bd + ab32934 commit 2275c13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Fieldtypes/CurrencyFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doefom\CurrencyFieldtype\Fieldtypes;

use Doefom\CurrencyFieldtype\Filters\CurrencyFilter;
use Doefom\CurrencyFieldtype\Models\Currency;
use Doefom\CurrencyFieldtype\Utils\Currencies;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -202,9 +203,17 @@ private function getSubUnitFactor(): int
*
* @return bool The sub-unit boolean of the currency.
*/
private function usesSubUnitStorage(): bool
public function usesSubUnitStorage(): bool
{
return Arr::get($this->field()->config(), 'store_sub_units', false);
}

/**
* Sets the custom filter type used in list view filters.
* @return CurrencyFilter
*/
public function filter()
{
return new CurrencyFilter($this);
}
}
22 changes: 22 additions & 0 deletions src/Filters/CurrencyFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Doefom\CurrencyFieldtype\Filters;

use Statamic\Query\Scopes\Filters\Fields\Number as NumberFilter;

class CurrencyFilter extends NumberFilter
{
protected function valueFieldtype()
{
return 'currency';
}

public function apply($query, $handle, $values)
{
if($this->fieldtype->usesSubUnitStorage()){
$values['value'] *= 100;
}

return parent::apply($query, $handle, $values);
}
}

0 comments on commit 2275c13

Please sign in to comment.