Skip to content

Commit

Permalink
Prevent TermsFilter from having to deal with empty strings:
Browse files Browse the repository at this point in the history
filter them out before parsing them to int
in form, convert taxonomies to OptionGroups rather than options with no value
  • Loading branch information
MatteoPiovanelli-Laser committed Oct 30, 2023
1 parent 03884cb commit 6b38c41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public void ApplyFilter(dynamic context) {
var termIds = (string)context.State.TermIds;

if (!String.IsNullOrEmpty(termIds)) {
var ids = termIds.Split(new[] { ',' }).Select(Int32.Parse).ToArray();
var ids = termIds.Split(new[] { ',' })
// Int32.Parse throws for empty strings
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(Int32.Parse).ToArray();

if (ids.Length == 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ public void Describe(dynamic context) {
);

foreach (var taxonomy in _taxonomyService.GetTaxonomies()) {
f._Terms.Add(new SelectListItem { Value = String.Empty, Text = taxonomy.Name });
var tGroup = new SelectListGroup { Name = taxonomy.Name };
f._Terms.Add(tGroup);
foreach (var term in _taxonomyService.GetTerms(taxonomy.Id)) {
var gap = new string('-', term.GetLevels());

if (gap.Length > 0) {
gap += " ";
}

f._Terms.Add(new SelectListItem { Value = term.Id.ToString(), Text = gap + term.Name });
f._Terms.Add(new SelectListItem {
Value = term.Id.ToString(),
Text = gap + term.Name,
Group = tGroup
});
}
}

Expand Down

0 comments on commit 6b38c41

Please sign in to comment.