Skip to content

Commit

Permalink
Enable regions with the same name to be repeated multiple times on a …
Browse files Browse the repository at this point in the history
…page
  • Loading branch information
willprice76 committed Jul 25, 2016
1 parent cae0342 commit 4aa3521
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
24 changes: 21 additions & 3 deletions Sdl.Web.Common/Models/RegionModel.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand Down Expand Up @@ -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
/// <summary>
/// Initializes a new <see cref="RegionModel"/> instance.
Expand All @@ -80,6 +93,11 @@ public RegionModel(string name, string qualifiedViewName)
}
#endregion

public String GetNameWithPostfix(int counter)
{
return String.Format("___{0}", counter);
}

#region Overrides

/// <summary>
Expand All @@ -89,7 +107,7 @@ public RegionModel(string name, string qualifiedViewName)
/// <returns>The XPM markup.</returns>
public override string GetXpmMarkup(Localization localization)
{
XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(Name);
XpmRegion xpmRegion = localization.GetXpmRegionConfiguration(this.NameWithoutPostfix);
if (xpmRegion == null)
{
return string.Empty;
Expand Down
68 changes: 47 additions & 21 deletions Sdl.Web.Tridion/Mapping/DefaultModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, int> duplicateRegionCounter = new Dictionary<string, int>();
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));
}
}

Expand Down Expand Up @@ -133,6 +117,48 @@ public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumera
}
}

private RegionModel GetRegionForEntity(RegionModelSet regions, IComponentPresentation cp, RegionModel currentRegion, Dictionary<String,int> 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))
Expand Down

0 comments on commit 4aa3521

Please sign in to comment.