Skip to content

Commit

Permalink
Implemented ability to add localizations of selected terms to filter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoPiovanelli-Laser authored Jan 19, 2024
1 parent d943fbd commit 97648ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Orchard.ContentManagement;
using Orchard.Events;
using Orchard.Localization;
using Orchard.Localization.Services;
using Orchard.Taxonomies.Drivers;

namespace Orchard.Taxonomies.Projections {
public interface IFilterProvider : IEventHandler {
Expand All @@ -14,10 +16,13 @@ public interface IFilterProvider : IEventHandler {

public class TermsFilter : IFilterProvider {
private readonly ITaxonomyService _taxonomyService;
private readonly IWorkContextAccessor _workContextAccessor;
private int _termsFilterId;

public TermsFilter(ITaxonomyService taxonomyService) {
public TermsFilter(ITaxonomyService taxonomyService,
IWorkContextAccessor workContextAccessor) {
_taxonomyService = taxonomyService;
_workContextAccessor = workContextAccessor;
T = NullLocalizer.Instance;
}

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

var terms = ids.Select(_taxonomyService.GetTerm).ToList();

bool.TryParse(context.State.TranslateTerms?.Value, out bool translateTerms);
if (translateTerms &&
_workContextAccessor.GetContext().TryResolve<ILocalizationService>(out var localizationService)) {
var localizedTerms = new List<TermPart>();
foreach (var termPart in terms) {
localizedTerms.AddRange(
localizationService.GetLocalizations(termPart)
.Select(l => l.As<TermPart>()));
}
terms.AddRange(localizedTerms);
terms = terms.Distinct(new TermPartComparer()).ToList();
}

var allChildren = new List<TermPart>();
bool.TryParse(context.State.ExcludeChildren?.Value, out bool excludeChildren);
foreach (var term in terms) {
bool.TryParse(context.State.ExcludeChildren?.Value, out bool excludeChildren);
if (!excludeChildren)
if (term == null) {
continue;
}
allChildren.Add(term);
if (!excludeChildren) {
allChildren.AddRange(_taxonomyService.GetChildren(term));
if (term != null)
allChildren.Add(term);
}
}

allChildren = allChildren.Distinct().ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void Describe(dynamic context) {
Id: "ExcludeChildren", Name: "ExcludeChildren",
Title: T("Automatically exclude children terms in filtering"),
Value: "true"
),
_TranslateTerms: Shape.Checkbox(
Id: "TranslateTerms", Name: "TranslateTerms",
Title: T("Automatically include terms' localizations in filtering"),
Value: "true"
)
);
Expand Down

0 comments on commit 97648ed

Please sign in to comment.