From 4455c66a64a0db250e687f50dc5122a30b73d119 Mon Sep 17 00:00:00 2001 From: Andrea Piovanelli <83577153+AndreaPiovanelliLaser@users.noreply.github.com> Date: Fri, 31 Mar 2023 09:44:26 +0200 Subject: [PATCH] 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. --- .../Controllers/LocalizedTaxonomyController.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Controllers/LocalizedTaxonomyController.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Controllers/LocalizedTaxonomyController.cs index bdff57de7..d21fc7e11 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Controllers/LocalizedTaxonomyController.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Controllers/LocalizedTaxonomyController.cs @@ -66,8 +66,15 @@ namespace Orchard.Taxonomies.Controllers { var firstTermForCulture = appliedTerms.FirstOrDefault(x => x.As() != null && x.As().Culture != null && x.As().Culture.Culture == culture); if (firstTermForCulture != null) { 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 { DisplayName = taxonomyField.DisplayName,