Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Configuration Support #79

Open
wants to merge 2 commits into
base: v9/dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class MeganavValueConverter : PropertyValueConverterBase
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly PublishedElementFactory _publishedElementFactory;

private MeganavConfiguration _config;
private Dictionary<int, MeganavConfiguration> _configs;

public MeganavValueConverter(IPublishedUrlProvider publishedUrlProvider, IVariationContextAccessor variationContextAccessor, IUmbracoContextFactory umbracoContextFactory, PublishedElementFactory publishedElementFactory)
{
Expand All @@ -42,7 +42,7 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType)

public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
EnsureConfiguration(propertyType);
var config = EnsureConfiguration(propertyType);

if (!(inter is string value))
{
Expand All @@ -53,18 +53,18 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub

using (var context = _umbracoContextFactory.EnsureUmbracoContext())
{
var items = BuildItems(context.UmbracoContext, entities);
var items = BuildItems(context.UmbracoContext, entities, config);

if (_config.MaxItems.HasValue == true)
if (config?.MaxItems.HasValue == true)
{
return items.Take(_config.MaxItems.Value);
return items.Take(config.MaxItems.Value);
}

return items;
}
}

private IEnumerable<MeganavItem> BuildItems(IUmbracoContext umbracoContext, IEnumerable<MeganavEntity> entities, int level = 0)
private IEnumerable<MeganavItem> BuildItems(IUmbracoContext umbracoContext, IEnumerable<MeganavEntity> entities, MeganavConfiguration config, int level = 0)
{
foreach (var entity in entities)
{
Expand All @@ -83,7 +83,7 @@ private IEnumerable<MeganavItem> BuildItems(IUmbracoContext umbracoContext, IEnu

if (entity.ItemTypeId != null)
{
var itemType = _config.ItemTypes.FirstOrDefault(x => x.Id == entity.ItemTypeId.Value);
var itemType = config.ItemTypes.FirstOrDefault(x => x.Id == entity.ItemTypeId.Value);

if (itemType != null)
{
Expand Down Expand Up @@ -134,19 +134,32 @@ private IEnumerable<MeganavItem> BuildItems(IUmbracoContext umbracoContext, IEnu

if (entity.Children != null)
{
item.Children = BuildItems(umbracoContext, entity.Children, level + 1);
item.Children = BuildItems(umbracoContext, entity.Children, config, level + 1);
}

yield return item;
}
}

private void EnsureConfiguration(IPublishedPropertyType propertyType)
private MeganavConfiguration EnsureConfiguration(IPublishedPropertyType propertyType)
{
if (_config == null)
var dataTypeId = propertyType?.DataType?.Id ?? 0;

_configs ??= new Dictionary<int, MeganavConfiguration>();

if (!_configs.TryGetValue(dataTypeId, out var config) || config == null)
{
_config = propertyType.DataType.ConfigurationAs<MeganavConfiguration>();
config = propertyType?.DataType?.ConfigurationAs<MeganavConfiguration>();
_configs.Add(dataTypeId, config);
}

config ??= new MeganavConfiguration
{
ItemTypes = new List<MeganavItemType>()
};

return config;

}
}
}