Skip to content

Commit 97648ed

Browse files
author
Matteo Piovanelli
authored
Implemented ability to add localizations of selected terms to filter (#8730)
1 parent d943fbd commit 97648ed

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/Orchard.Web/Modules/Orchard.Taxonomies/Projections/TermsFilter.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Orchard.ContentManagement;
77
using Orchard.Events;
88
using Orchard.Localization;
9+
using Orchard.Localization.Services;
10+
using Orchard.Taxonomies.Drivers;
911

1012
namespace Orchard.Taxonomies.Projections {
1113
public interface IFilterProvider : IEventHandler {
@@ -14,10 +16,13 @@ public interface IFilterProvider : IEventHandler {
1416

1517
public class TermsFilter : IFilterProvider {
1618
private readonly ITaxonomyService _taxonomyService;
19+
private readonly IWorkContextAccessor _workContextAccessor;
1720
private int _termsFilterId;
1821

19-
public TermsFilter(ITaxonomyService taxonomyService) {
22+
public TermsFilter(ITaxonomyService taxonomyService,
23+
IWorkContextAccessor workContextAccessor) {
2024
_taxonomyService = taxonomyService;
25+
_workContextAccessor = workContextAccessor;
2126
T = NullLocalizer.Instance;
2227
}
2328

@@ -48,13 +53,30 @@ public void ApplyFilter(dynamic context) {
4853
int op = Convert.ToInt32(context.State.Operator);
4954

5055
var terms = ids.Select(_taxonomyService.GetTerm).ToList();
56+
57+
bool.TryParse(context.State.TranslateTerms?.Value, out bool translateTerms);
58+
if (translateTerms &&
59+
_workContextAccessor.GetContext().TryResolve<ILocalizationService>(out var localizationService)) {
60+
var localizedTerms = new List<TermPart>();
61+
foreach (var termPart in terms) {
62+
localizedTerms.AddRange(
63+
localizationService.GetLocalizations(termPart)
64+
.Select(l => l.As<TermPart>()));
65+
}
66+
terms.AddRange(localizedTerms);
67+
terms = terms.Distinct(new TermPartComparer()).ToList();
68+
}
69+
5170
var allChildren = new List<TermPart>();
71+
bool.TryParse(context.State.ExcludeChildren?.Value, out bool excludeChildren);
5272
foreach (var term in terms) {
53-
bool.TryParse(context.State.ExcludeChildren?.Value, out bool excludeChildren);
54-
if (!excludeChildren)
73+
if (term == null) {
74+
continue;
75+
}
76+
allChildren.Add(term);
77+
if (!excludeChildren) {
5578
allChildren.AddRange(_taxonomyService.GetChildren(term));
56-
if (term != null)
57-
allChildren.Add(term);
79+
}
5880
}
5981

6082
allChildren = allChildren.Distinct().ToList();

src/Orchard.Web/Modules/Orchard.Taxonomies/Projections/TermsFilterForms.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public void Describe(dynamic context) {
5353
Id: "ExcludeChildren", Name: "ExcludeChildren",
5454
Title: T("Automatically exclude children terms in filtering"),
5555
Value: "true"
56+
),
57+
_TranslateTerms: Shape.Checkbox(
58+
Id: "TranslateTerms", Name: "TranslateTerms",
59+
Title: T("Automatically include terms' localizations in filtering"),
60+
Value: "true"
5661
)
5762
);
5863

0 commit comments

Comments
 (0)