Skip to content

Commit

Permalink
Prevent TermsFilter from having to deal with empty strings (#8727)
Browse files Browse the repository at this point in the history
* Prevent TermsFilter from having to deal with empty strings:
filter them out before parsing them to int
in form, convert taxonomies to OptionGroups rather than options with no value

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

Co-authored-by: Sébastien Ros <[email protected]>

---------

Co-authored-by: Sébastien Ros <[email protected]>
  • Loading branch information
MatteoPiovanelli-Laser and sebastienros authored Jan 15, 2024
1 parent 9644ced commit e013e00
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[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
// 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 e013e00

Please sign in to comment.