From 6c590027c7dd4de369c7c5dc3238f7bd1d6a84ea Mon Sep 17 00:00:00 2001 From: Will Price Date: Mon, 25 Jul 2016 15:08:16 +0200 Subject: [PATCH 1/9] Enable regions with the same name to be repeated multiple times on a page --- Sdl.Web.Common/Models/RegionModel.cs | 24 ++++++- .../Mapping/DefaultModelBuilder.cs | 68 +++++++++++++------ 2 files changed, 68 insertions(+), 24 deletions(-) mode change 100644 => 100755 Sdl.Web.Common/Models/RegionModel.cs diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs old mode 100644 new mode 100755 index 55b92cfb..39454254 --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using System.Linq; using Sdl.Web.Common.Configuration; - +using System.Text.RegularExpressions; +using System; + namespace Sdl.Web.Common.Models { /// @@ -54,7 +56,18 @@ public RegionModelSet Regions } } - + public String NameWithoutPostfix + { + get + { + var match = Regex.Match(Name, @"(.*)___\d+"); + if (match.Success) + { + return match.Groups[1].Value; + } + return Name; + } + } #region Constructors /// /// Initializes a new instance. @@ -80,6 +93,11 @@ public RegionModel(string name, string qualifiedViewName) } #endregion + public String GetNameWithPostfix(int counter) + { + return String.Format("___{0}", counter); + } + #region Overrides /// @@ -89,7 +107,7 @@ public RegionModel(string name, string qualifiedViewName) /// The XPM markup. public override string GetXpmMarkup(Localization localization) { - XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(Name); + XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(this.NameWithoutPostfix); if (xpmRegion == null) { return string.Empty; diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 88af2323..8986c983 100644 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -51,40 +51,24 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera // Create Regions/Entities from Component Presentations IConditionalEntityEvaluator conditionalEntityEvaluator = SiteConfiguration.ConditionalEntityEvaluator; + RegionModel currentRegion = null; + Dictionary duplicateRegionCounter = new Dictionary(); foreach (IComponentPresentation cp in page.ComponentPresentations) { - MvcData cpRegionMvcData = GetRegionMvcData(cp); - string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; - RegionModel region; - if (regions.TryGetValue(regionName, out region)) - { - // Region already exists in Page Model; MVC data should match. - if (!region.MvcData.Equals(cpRegionMvcData)) - { - Log.Warn("Region '{0}' is defined with conflicting MVC data: [{1}] and [{2}]. Using the former.", region.Name, region.MvcData, cpRegionMvcData); - } - } - else - { - // Region does not exist in Page Model yet; create Region Model and add it. - region = CreateRegionModel(cpRegionMvcData); - regions.Add(region); - } - + currentRegion = GetRegionForEntity(regions, cp, currentRegion, duplicateRegionCounter); try { EntityModel entity = ModelBuilderPipeline.CreateEntityModel(cp, localization); - if (conditionalEntityEvaluator == null || conditionalEntityEvaluator.IncludeEntity(entity)) { - region.Entities.Add(entity); + currentRegion.Entities.Add(entity); } } catch (Exception ex) { // If there is a problem mapping an Entity, we replace it with an ExceptionEntity which holds the error details and carry on. Log.Error(ex); - region.Entities.Add(new ExceptionEntity(ex)); + currentRegion.Entities.Add(new ExceptionEntity(ex)); } } @@ -133,6 +117,48 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera } } + private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresentation cp, RegionModel currentRegion, Dictionary duplicateRegionCounter) + { + MvcData cpRegionMvcData = GetRegionMvcData(cp); + string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; + string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; + string duplicateRegionPostfix = null; + RegionModel region = null; + //If we are still in the same region, reuse it + if (regionName==currentRegionName) + { + region = currentRegion; + } + //We already had this region, but there has been something else inbetween, so we need to create a new (postfixed) one + else if (regions.ContainsKey(regionName)) + { + int counter = 1; + if (duplicateRegionCounter.ContainsKey(regionName)) + { + counter = duplicateRegionCounter[regionName] + 1; + duplicateRegionCounter[regionName] = counter; + } + else + { + duplicateRegionCounter.Add(regionName, counter); + } + duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); + } + //New region required + if (region == null) + { + // Region does not exist in Page Model yet; create Region Model and add it. + if (!String.IsNullOrEmpty(duplicateRegionPostfix)) + { + cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); + } + region = CreateRegionModel(cpRegionMvcData); + regions.Add(region); + currentRegion = region; + } + return region; + } + public virtual void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization) { using (new Tracer(entityModel, cp, localization)) From 816bfe2c02f7de97fb4644a685c8ab55322778d2 Mon Sep 17 00:00:00 2001 From: Will Price Date: Mon, 25 Jul 2016 16:08:43 +0200 Subject: [PATCH 2/9] Clearer separation of duplicate region naming logic --- Sdl.Web.Common/Models/RegionModel.cs | 2 +- Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) mode change 100644 => 100755 Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs index 39454254..d64a1fcf 100755 --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -95,7 +95,7 @@ public RegionModel(string name, string qualifiedViewName) public String GetNameWithPostfix(int counter) { - return String.Format("___{0}", counter); + return String.Format("{0}___{1}", this.Name, counter); } #region Overrides diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs old mode 100644 new mode 100755 index 8986c983..2f1458bd --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -122,7 +122,7 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent MvcData cpRegionMvcData = GetRegionMvcData(cp); string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; - string duplicateRegionPostfix = null; + string duplicateRegionName = null; RegionModel region = null; //If we are still in the same region, reuse it if (regionName==currentRegionName) @@ -142,15 +142,15 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent { duplicateRegionCounter.Add(regionName, counter); } - duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); + duplicateRegionName = regions[regionName].GetNameWithPostfix(counter); } //New region required if (region == null) { // Region does not exist in Page Model yet; create Region Model and add it. - if (!String.IsNullOrEmpty(duplicateRegionPostfix)) + if (!String.IsNullOrEmpty(duplicateRegionName)) { - cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); + cpRegionMvcData.RegionName = duplicateRegionName; } region = CreateRegionModel(cpRegionMvcData); regions.Add(region); From fe63bc61c15cef6c97b0cae08b9dcd3e88fb8e57 Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 11:58:08 +0200 Subject: [PATCH 3/9] Add route values from region mvc data --- Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) mode change 100644 => 100755 Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs diff --git a/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs b/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs old mode 100644 new mode 100755 index b1652bc3..5ec3e512 --- a/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs +++ b/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs @@ -381,9 +381,20 @@ public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel re MvcData mvcData = region.MvcData; string actionName = mvcData.ActionName ?? SiteConfiguration.GetRegionAction(); string controllerName = mvcData.ControllerName ?? SiteConfiguration.GetRegionController(); - string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); - - MvcHtmlString result = htmlHelper.Action(actionName, controllerName, new { Region = region, containerSize = containerSize, area = controllerAreaName }); + string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); + + RouteValueDictionary parameters = new RouteValueDictionary(); + parameters["containerSize"] = containerSize; + parameters["region"] = region; + parameters["area"] = controllerAreaName; + if (mvcData.RouteValues != null) + { + foreach (string key in mvcData.RouteValues.Keys) + { + parameters[key] = mvcData.RouteValues[key]; + } + } + MvcHtmlString result = htmlHelper.Action(actionName, controllerName, parameters); if (WebRequestContext.IsPreview) { From 6f49c839a04af7c14c48d7942462bc7dc2cde68e Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 12:34:32 +0200 Subject: [PATCH 4/9] Revert "Clearer separation of duplicate region naming logic" This reverts commit 2116290f7a6e068a2bc2cf75393a9c307d636ba9. --- Sdl.Web.Common/Models/RegionModel.cs | 2 +- Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs index d64a1fcf..39454254 100755 --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -95,7 +95,7 @@ public RegionModel(string name, string qualifiedViewName) public String GetNameWithPostfix(int counter) { - return String.Format("{0}___{1}", this.Name, counter); + return String.Format("___{0}", counter); } #region Overrides diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 2f1458bd..8986c983 100755 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -122,7 +122,7 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent MvcData cpRegionMvcData = GetRegionMvcData(cp); string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; - string duplicateRegionName = null; + string duplicateRegionPostfix = null; RegionModel region = null; //If we are still in the same region, reuse it if (regionName==currentRegionName) @@ -142,15 +142,15 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent { duplicateRegionCounter.Add(regionName, counter); } - duplicateRegionName = regions[regionName].GetNameWithPostfix(counter); + duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); } //New region required if (region == null) { // Region does not exist in Page Model yet; create Region Model and add it. - if (!String.IsNullOrEmpty(duplicateRegionName)) + if (!String.IsNullOrEmpty(duplicateRegionPostfix)) { - cpRegionMvcData.RegionName = duplicateRegionName; + cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); } region = CreateRegionModel(cpRegionMvcData); regions.Add(region); From 77aded1c96616b2e9d8ac386745221fd6401a632 Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 12:34:43 +0200 Subject: [PATCH 5/9] Revert "Enable regions with the same name to be repeated multiple times on a page" This reverts commit 4aa352174f22d650a6dd9e09e4d216d668a377c3. --- Sdl.Web.Common/Models/RegionModel.cs | 24 +------ .../Mapping/DefaultModelBuilder.cs | 68 ++++++------------- 2 files changed, 24 insertions(+), 68 deletions(-) mode change 100755 => 100644 Sdl.Web.Common/Models/RegionModel.cs diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs old mode 100755 new mode 100644 index 39454254..55b92cfb --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -1,9 +1,7 @@ using System.Collections.Generic; using System.Linq; using Sdl.Web.Common.Configuration; -using System.Text.RegularExpressions; -using System; - + namespace Sdl.Web.Common.Models { /// @@ -56,18 +54,7 @@ public RegionModelSet Regions } } - public String NameWithoutPostfix - { - get - { - var match = Regex.Match(Name, @"(.*)___\d+"); - if (match.Success) - { - return match.Groups[1].Value; - } - return Name; - } - } + #region Constructors /// /// Initializes a new instance. @@ -93,11 +80,6 @@ public RegionModel(string name, string qualifiedViewName) } #endregion - public String GetNameWithPostfix(int counter) - { - return String.Format("___{0}", counter); - } - #region Overrides /// @@ -107,7 +89,7 @@ public String GetNameWithPostfix(int counter) /// The XPM markup. public override string GetXpmMarkup(Localization localization) { - XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(this.NameWithoutPostfix); + XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(Name); if (xpmRegion == null) { return string.Empty; diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 8986c983..88af2323 100755 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -51,24 +51,40 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera // Create Regions/Entities from Component Presentations IConditionalEntityEvaluator conditionalEntityEvaluator = SiteConfiguration.ConditionalEntityEvaluator; - RegionModel currentRegion = null; - Dictionary duplicateRegionCounter = new Dictionary(); foreach (IComponentPresentation cp in page.ComponentPresentations) { - currentRegion = GetRegionForEntity(regions, cp, currentRegion, duplicateRegionCounter); + MvcData cpRegionMvcData = GetRegionMvcData(cp); + string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; + RegionModel region; + if (regions.TryGetValue(regionName, out region)) + { + // Region already exists in Page Model; MVC data should match. + if (!region.MvcData.Equals(cpRegionMvcData)) + { + Log.Warn("Region '{0}' is defined with conflicting MVC data: [{1}] and [{2}]. Using the former.", region.Name, region.MvcData, cpRegionMvcData); + } + } + else + { + // Region does not exist in Page Model yet; create Region Model and add it. + region = CreateRegionModel(cpRegionMvcData); + regions.Add(region); + } + try { EntityModel entity = ModelBuilderPipeline.CreateEntityModel(cp, localization); + if (conditionalEntityEvaluator == null || conditionalEntityEvaluator.IncludeEntity(entity)) { - currentRegion.Entities.Add(entity); + region.Entities.Add(entity); } } catch (Exception ex) { // If there is a problem mapping an Entity, we replace it with an ExceptionEntity which holds the error details and carry on. Log.Error(ex); - currentRegion.Entities.Add(new ExceptionEntity(ex)); + region.Entities.Add(new ExceptionEntity(ex)); } } @@ -117,48 +133,6 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera } } - private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresentation cp, RegionModel currentRegion, Dictionary duplicateRegionCounter) - { - MvcData cpRegionMvcData = GetRegionMvcData(cp); - string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; - string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; - string duplicateRegionPostfix = null; - RegionModel region = null; - //If we are still in the same region, reuse it - if (regionName==currentRegionName) - { - region = currentRegion; - } - //We already had this region, but there has been something else inbetween, so we need to create a new (postfixed) one - else if (regions.ContainsKey(regionName)) - { - int counter = 1; - if (duplicateRegionCounter.ContainsKey(regionName)) - { - counter = duplicateRegionCounter[regionName] + 1; - duplicateRegionCounter[regionName] = counter; - } - else - { - duplicateRegionCounter.Add(regionName, counter); - } - duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); - } - //New region required - if (region == null) - { - // Region does not exist in Page Model yet; create Region Model and add it. - if (!String.IsNullOrEmpty(duplicateRegionPostfix)) - { - cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); - } - region = CreateRegionModel(cpRegionMvcData); - regions.Add(region); - currentRegion = region; - } - return region; - } - public virtual void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization) { using (new Tracer(entityModel, cp, localization)) From f16dd316631880a3c3ceaf7a6f1f7b3fae41074c Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 14:27:15 +0200 Subject: [PATCH 6/9] Revert "Revert "Enable regions with the same name to be repeated multiple times on a page"" This reverts commit 77aded1c96616b2e9d8ac386745221fd6401a632. --- Sdl.Web.Common/Models/RegionModel.cs | 24 ++++++- .../Mapping/DefaultModelBuilder.cs | 68 +++++++++++++------ 2 files changed, 68 insertions(+), 24 deletions(-) mode change 100644 => 100755 Sdl.Web.Common/Models/RegionModel.cs diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs old mode 100644 new mode 100755 index 55b92cfb..39454254 --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using System.Linq; using Sdl.Web.Common.Configuration; - +using System.Text.RegularExpressions; +using System; + namespace Sdl.Web.Common.Models { /// @@ -54,7 +56,18 @@ public RegionModelSet Regions } } - + public String NameWithoutPostfix + { + get + { + var match = Regex.Match(Name, @"(.*)___\d+"); + if (match.Success) + { + return match.Groups[1].Value; + } + return Name; + } + } #region Constructors /// /// Initializes a new instance. @@ -80,6 +93,11 @@ public RegionModel(string name, string qualifiedViewName) } #endregion + public String GetNameWithPostfix(int counter) + { + return String.Format("___{0}", counter); + } + #region Overrides /// @@ -89,7 +107,7 @@ public RegionModel(string name, string qualifiedViewName) /// The XPM markup. public override string GetXpmMarkup(Localization localization) { - XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(Name); + XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(this.NameWithoutPostfix); if (xpmRegion == null) { return string.Empty; diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 88af2323..8986c983 100755 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -51,40 +51,24 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera // Create Regions/Entities from Component Presentations IConditionalEntityEvaluator conditionalEntityEvaluator = SiteConfiguration.ConditionalEntityEvaluator; + RegionModel currentRegion = null; + Dictionary duplicateRegionCounter = new Dictionary(); foreach (IComponentPresentation cp in page.ComponentPresentations) { - MvcData cpRegionMvcData = GetRegionMvcData(cp); - string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; - RegionModel region; - if (regions.TryGetValue(regionName, out region)) - { - // Region already exists in Page Model; MVC data should match. - if (!region.MvcData.Equals(cpRegionMvcData)) - { - Log.Warn("Region '{0}' is defined with conflicting MVC data: [{1}] and [{2}]. Using the former.", region.Name, region.MvcData, cpRegionMvcData); - } - } - else - { - // Region does not exist in Page Model yet; create Region Model and add it. - region = CreateRegionModel(cpRegionMvcData); - regions.Add(region); - } - + currentRegion = GetRegionForEntity(regions, cp, currentRegion, duplicateRegionCounter); try { EntityModel entity = ModelBuilderPipeline.CreateEntityModel(cp, localization); - if (conditionalEntityEvaluator == null || conditionalEntityEvaluator.IncludeEntity(entity)) { - region.Entities.Add(entity); + currentRegion.Entities.Add(entity); } } catch (Exception ex) { // If there is a problem mapping an Entity, we replace it with an ExceptionEntity which holds the error details and carry on. Log.Error(ex); - region.Entities.Add(new ExceptionEntity(ex)); + currentRegion.Entities.Add(new ExceptionEntity(ex)); } } @@ -133,6 +117,48 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera } } + private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresentation cp, RegionModel currentRegion, Dictionary duplicateRegionCounter) + { + MvcData cpRegionMvcData = GetRegionMvcData(cp); + string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; + string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; + string duplicateRegionPostfix = null; + RegionModel region = null; + //If we are still in the same region, reuse it + if (regionName==currentRegionName) + { + region = currentRegion; + } + //We already had this region, but there has been something else inbetween, so we need to create a new (postfixed) one + else if (regions.ContainsKey(regionName)) + { + int counter = 1; + if (duplicateRegionCounter.ContainsKey(regionName)) + { + counter = duplicateRegionCounter[regionName] + 1; + duplicateRegionCounter[regionName] = counter; + } + else + { + duplicateRegionCounter.Add(regionName, counter); + } + duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); + } + //New region required + if (region == null) + { + // Region does not exist in Page Model yet; create Region Model and add it. + if (!String.IsNullOrEmpty(duplicateRegionPostfix)) + { + cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); + } + region = CreateRegionModel(cpRegionMvcData); + regions.Add(region); + currentRegion = region; + } + return region; + } + public virtual void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization) { using (new Tracer(entityModel, cp, localization)) From beba41d398c466bdb5e926ffc6193afb54d8b8bf Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 14:27:28 +0200 Subject: [PATCH 7/9] Revert "Revert "Clearer separation of duplicate region naming logic"" This reverts commit 6f49c839a04af7c14c48d7942462bc7dc2cde68e. --- Sdl.Web.Common/Models/RegionModel.cs | 2 +- Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sdl.Web.Common/Models/RegionModel.cs b/Sdl.Web.Common/Models/RegionModel.cs index 39454254..d64a1fcf 100755 --- a/Sdl.Web.Common/Models/RegionModel.cs +++ b/Sdl.Web.Common/Models/RegionModel.cs @@ -95,7 +95,7 @@ public RegionModel(string name, string qualifiedViewName) public String GetNameWithPostfix(int counter) { - return String.Format("___{0}", counter); + return String.Format("{0}___{1}", this.Name, counter); } #region Overrides diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 8986c983..2f1458bd 100755 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -122,7 +122,7 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent MvcData cpRegionMvcData = GetRegionMvcData(cp); string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName; string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; - string duplicateRegionPostfix = null; + string duplicateRegionName = null; RegionModel region = null; //If we are still in the same region, reuse it if (regionName==currentRegionName) @@ -142,15 +142,15 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent { duplicateRegionCounter.Add(regionName, counter); } - duplicateRegionPostfix = regions[regionName].GetNameWithPostfix(counter); + duplicateRegionName = regions[regionName].GetNameWithPostfix(counter); } //New region required if (region == null) { // Region does not exist in Page Model yet; create Region Model and add it. - if (!String.IsNullOrEmpty(duplicateRegionPostfix)) + if (!String.IsNullOrEmpty(duplicateRegionName)) { - cpRegionMvcData.RegionName = String.Format("{0}{1}", regionName, duplicateRegionPostfix); + cpRegionMvcData.RegionName = duplicateRegionName; } region = CreateRegionModel(cpRegionMvcData); regions.Add(region); From 11626579797c07534293c9a9891bbea6b8cc6d4a Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 14:27:38 +0200 Subject: [PATCH 8/9] Revert "Add route values from region mvc data" This reverts commit fe63bc61c15cef6c97b0cae08b9dcd3e88fb8e57. --- Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) mode change 100755 => 100644 Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs diff --git a/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs b/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs old mode 100755 new mode 100644 index 5ec3e512..b1652bc3 --- a/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs +++ b/Sdl.Web.Mvc/Html/HtmlHelperExtensions.cs @@ -381,20 +381,9 @@ public static MvcHtmlString DxaRegion(this HtmlHelper htmlHelper, RegionModel re MvcData mvcData = region.MvcData; string actionName = mvcData.ActionName ?? SiteConfiguration.GetRegionAction(); string controllerName = mvcData.ControllerName ?? SiteConfiguration.GetRegionController(); - string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); - - RouteValueDictionary parameters = new RouteValueDictionary(); - parameters["containerSize"] = containerSize; - parameters["region"] = region; - parameters["area"] = controllerAreaName; - if (mvcData.RouteValues != null) - { - foreach (string key in mvcData.RouteValues.Keys) - { - parameters[key] = mvcData.RouteValues[key]; - } - } - MvcHtmlString result = htmlHelper.Action(actionName, controllerName, parameters); + string controllerAreaName = mvcData.ControllerAreaName ?? SiteConfiguration.GetDefaultModuleName(); + + MvcHtmlString result = htmlHelper.Action(actionName, controllerName, new { Region = region, containerSize = containerSize, area = controllerAreaName }); if (WebRequestContext.IsPreview) { From 01e7198ead37d6741c197e9f2ec1feb1169aaea5 Mon Sep 17 00:00:00 2001 From: Will Price Date: Tue, 26 Jul 2016 21:02:13 +0200 Subject: [PATCH 9/9] Use a change in region MVC data as a trigger to start a new region - now duplicate regions are possible, this enables us to have, for example two consecutive 3-Column regions with different parameters (so separate view configuration) --- Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs index 2f1458bd..77794578 100755 --- a/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs +++ b/Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs @@ -124,13 +124,21 @@ private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresent string currentRegionName = currentRegion!=null ? currentRegion.NameWithoutPostfix : null; string duplicateRegionName = null; RegionModel region = null; - //If we are still in the same region, reuse it + //If we are still in the same region, reuse it, unless the mvc data is different, + //in which case we start a new region of the same name if (regionName==currentRegionName) - { - region = currentRegion; + { + if (currentRegion.MvcData.Equals(cpRegionMvcData)) + { + return currentRegion; + } + else + { + Log.Debug("Region '{0}' is defined with different MVC data: [{1}] and [{2}]. Starting a new region.", regionName, currentRegion.MvcData, cpRegionMvcData); + } } //We already had this region, but there has been something else inbetween, so we need to create a new (postfixed) one - else if (regions.ContainsKey(regionName)) + if (regions.ContainsKey(regionName)) { int counter = 1; if (duplicateRegionCounter.ContainsKey(regionName))