Avoid SingleTermId being null if at least one term is checked (#8662)

* If no term with the proper culture is found, SingleTermId is the id of the first valid checked term.

* Correction on firstTermForCulture query if there is no LocalizationPart.
Simplified query to check selected terms.
This commit is contained in:
Andrea Piovanelli
2023-03-31 09:44:26 +02:00
committed by GitHub
parent 2dd08dbc04
commit 4455c66a64

View File

@@ -66,8 +66,15 @@ namespace Orchard.Taxonomies.Controllers {
var firstTermForCulture = appliedTerms.FirstOrDefault(x => x.As<LocalizationPart>() != null && x.As<LocalizationPart>().Culture != null && x.As<LocalizationPart>().Culture.Culture == culture); var firstTermForCulture = appliedTerms.FirstOrDefault(x => x.As<LocalizationPart>() != null && x.As<LocalizationPart>().Culture != null && x.As<LocalizationPart>().Culture.Culture == culture);
if (firstTermForCulture != null) { if (firstTermForCulture != null) {
firstTermIdForCulture = firstTermForCulture.Id; firstTermIdForCulture = firstTermForCulture.Id;
} else {
// If there is no valid localization, firstTermForCulture is null.
// To avoid that, use the first checked term (if any is checked).
firstTermForCulture = appliedTerms.FirstOrDefault(t => terms.Any(x => x.Id == t.Id));
if (firstTermForCulture != null) {
firstTermIdForCulture = firstTermForCulture.Id;
}
} }
terms.ForEach(t => t.IsChecked = appliedTerms.Select(x => x.Id).Contains(t.Id)); terms.ForEach(t => t.IsChecked = appliedTerms.Any(x => x.Id == t.Id));
} }
viewModel = new TaxonomyFieldViewModel { viewModel = new TaxonomyFieldViewModel {
DisplayName = taxonomyField.DisplayName, DisplayName = taxonomyField.DisplayName,