Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for category filtering in Categories component #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
],
Comment on lines +56 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomainMazB three comments here:

  • I think the name categoryFilter is probably not accurate for what we are doing. Instead, this should probably be rootCategory, as technically we are specifying a root category to start nesting from.
  • I think those lang strings might be incorrect?
  • Should the default perhaps be blank so that the original functionality is maintained?

];
}

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomainMazB I'd recommend we continue nesting with getNested here, and leave it up to the template whether to display the full nested structure underneath the root category, or just simply show the direct children.

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

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