Skip to content

Commit ecc4628

Browse files
authored
Added possibility to filter by frontpage (#20)
1 parent 00367f1 commit ecc4628

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

extend.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Flarum\Api\Serializer\DiscussionSerializer;
1616
use Flarum\Discussion\Discussion;
1717
use Flarum\Discussion\Event\Saving;
18+
use Flarum\Discussion\Filter\DiscussionFilterer;
1819
use Flarum\Discussion\Search\DiscussionSearcher;
1920
use Flarum\Extend;
2021
use FoF\FrontPage\Gambits\FrontGambit;
@@ -34,6 +35,9 @@
3435
(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
3536
->addGambit(FrontGambit::class),
3637

38+
(new Extend\Filter(DiscussionFilterer::class))
39+
->addFilter(FrontGambit::class),
40+
3741
(new Extend\Model(Discussion::class))
3842
->dateAttribute('frontdate'),
3943

src/Gambits/FrontGambit.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,56 @@
1111

1212
namespace FoF\FrontPage\Gambits;
1313

14+
use Flarum\Filter\FilterInterface;
15+
use Flarum\Filter\FilterState;
1416
use Flarum\Query\AbstractQueryState;
1517
use Flarum\Search\AbstractRegexGambit;
18+
use Illuminate\Database\Query\Builder;
1619

17-
class FrontGambit extends AbstractRegexGambit
20+
class FrontGambit extends AbstractRegexGambit implements FilterInterface
1821
{
1922
/**
20-
* @return [type]
23+
* @return string
2124
*/
2225
public function getGambitPattern()
2326
{
2427
return 'is:frontpage';
2528
}
2629

30+
/**
31+
* @return string
32+
*/
33+
public function getFilterKey(): string
34+
{
35+
return 'frontpage';
36+
}
37+
2738
/**
2839
* @param AbstractQueryState $search
2940
* @param array $matches
3041
* @param mixed $negate
3142
*
32-
* @return [type]
43+
* @return void
3344
*/
3445
public function conditions(AbstractQueryState $search, array $matches, $negate)
3546
{
36-
$search->getQuery()->where('frontpage', !$negate);
47+
$this->constrain($search->getQuery(), $negate);
48+
}
49+
50+
/**
51+
* @param FilterState $search
52+
* @param string $filterValue
53+
* @param mixed $negate
54+
*
55+
* @return void
56+
*/
57+
public function filter(FilterState $filterState, string $filterValue, bool $negate)
58+
{
59+
$this->constrain($filterState->getQuery(), $negate);
60+
}
61+
62+
protected function constrain(Builder $query, bool $negate)
63+
{
64+
$query->where('frontpage', !$negate);
3765
}
3866
}

0 commit comments

Comments
 (0)