Skip to content

Instantly share code, notes, and snippets.

@Hangsolow
Created April 14, 2020 09:48
Show Gist options
  • Save Hangsolow/fd184adb06036229323a5b53de95dfd5 to your computer and use it in GitHub Desktop.
Save Hangsolow/fd184adb06036229323a5b53de95dfd5 to your computer and use it in GitHub Desktop.
ContentAreaPropertyModel
public class AbContentAreaPropertyModel : ContentAreaPropertyModel
{
private readonly Injected<IDisplayOptionService> _injectedDisplayOptionService;
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, bool excludePersonalizedContent) : base(propertyContentArea, excludePersonalizedContent)
{
}
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, bool excludePersonalizedContent, EPiServer.ContentApi.Core.ContentLoaderService contentLoaderService, EPiServer.ContentApi.Core.Serialization.IContentModelMapper contentModelMapper, EPiServer.Security.IContentAccessEvaluator accessEvaluator, EPiServer.ContentApi.Core.Security.ISecurityPrincipal principalAccessor) : base(propertyContentArea, excludePersonalizedContent, contentLoaderService, contentModelMapper, accessEvaluator, principalAccessor)
{
}
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, EPiServer.ContentApi.Core.Serialization.ConverterContext converterContext) : base(propertyContentArea, converterContext)
{
}
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, EPiServer.ContentApi.Core.Serialization.ConverterContext converterContext, EPiServer.ContentApi.Core.ContentLoaderService contentLoaderService, EPiServer.ContentApi.Core.Serialization.ContentConvertingService contentConvertingService, EPiServer.Security.IContentAccessEvaluator accessEvaluator, EPiServer.ContentApi.Core.Security.ISecurityPrincipal principalAccessor) : base(propertyContentArea, converterContext, contentLoaderService, contentConvertingService, accessEvaluator, principalAccessor)
{
}
public override object Flatten()
{
if (Value is null)
{
return null;
}
if (ExpandedValue is null)
{
//no matter what, Content Areas will always be expanded.
ExpandedValue = ExtractExpandedValue(ConverterContext.Language);
}
foreach (var content in ExpandedValue ?? Enumerable.Empty<ContentApiModel>())
{
//ExtractExpandedValue does not seem to carry the displayOption over from Value property
//so we do it ourselves
var displayOption = Value.FirstOrDefault(v => v.ContentLink.GuidValue == content.ContentLink.GuidValue)?.DisplayOption;
content.Properties["DisplayOption"] = displayOption;
//we have an enum where displayOptions names and css classes are defined, this adds the css class to 'DisplayOptionFallback'
//so the spa can choose to use it or translate the displayOption name to a class itself
if (!string.IsNullOrEmpty(displayOption) && TryGetDisplayOptionFallback(displayOption, out string fallback))
{
content.Properties["DisplayOptionFallback"] = fallback;
}
}
return ExpandedValue;
}
private bool TryGetDisplayOptionFallback(string item, out string value)
{
return _injectedDisplayOptionService.Service.DisplayOptions.TryGetValue(item, out value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment