Skip to content

Commit

Permalink
Add support for category filtering in Categories component
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMazB committed Sep 5, 2022
1 parent 3fba603 commit 1294620
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion components/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public function defineProperties()
'default' => 'blog/category',
'group' => 'winter.blog::lang.settings.group_links',
],
'categoryFilter' => [
'title' => 'winter.blog::lang.settings.posts_filter',
'description' => 'winter.blog::lang.settings.posts_filter_description',
'default' => '{{ :slug }}',
'type' => 'string',
],
];
}

Expand All @@ -74,7 +80,20 @@ public function onRun()
*/
protected function loadCategories()
{
$categories = BlogCategory::with('posts_count')->getNested();
$categoriesQuery = BlogCategory::with('posts_count');

if ($slug = $this->property('categoryFilter')) {
if ((new BlogCategory)->isClassExtendedWith('Winter.Translate.Behaviors.TranslatableModel')) {
$categoriesQuery->transWhere('slug', $slug);
} else {
$categoriesQuery->where('slug', $slug);
}

$categories = $categoriesQuery->first()->getChildren();
} else {
$categories = $categoriesQuery->getNested(false);
}

if (!$this->property('displayEmpty')) {
$iterator = function ($categories) use (&$iterator) {
return $categories->reject(function ($category) use (&$iterator) {
Expand Down

0 comments on commit 1294620

Please sign in to comment.