From f573a1723a355a4978b49f036871a44bea70024d Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Mon, 6 Jan 2025 11:21:31 -0500 Subject: [PATCH 1/2] Initial commit (broken) for multiple configs --- .../ValueConverters/MeganavValueConverter.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs b/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs index c663233..2aa3e82 100644 --- a/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs +++ b/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs @@ -20,7 +20,7 @@ internal class MeganavValueConverter : PropertyValueConverterBase private readonly IUmbracoContextFactory _umbracoContextFactory; private readonly PublishedElementFactory _publishedElementFactory; - private MeganavConfiguration _config; + private Dictionary _configs; public MeganavValueConverter(IPublishedUrlProvider publishedUrlProvider, IVariationContextAccessor variationContextAccessor, IUmbracoContextFactory umbracoContextFactory, PublishedElementFactory publishedElementFactory) { @@ -143,7 +143,16 @@ private IEnumerable BuildItems(IUmbracoContext umbracoContext, IEnu private void EnsureConfiguration(IPublishedPropertyType propertyType) { - if (_config == null) + var dataTypeAlias = propertyType?.DataType?.EditorAlias ?? string.Empty; + if (string.IsNullOrWhiteSpace(dataTypeAlias)) + dataTypeAlias = "default"; + + if (_configs == null) + { + _configs = new Dictionary(); + } + + if (_configs.ContainsKey(dataTypeAlias)) { { _config = propertyType.DataType.ConfigurationAs(); } From fdfeb8abacb154516383322cb195284d9a700df9 Mon Sep 17 00:00:00 2001 From: Patrick Joyce Date: Mon, 6 Jan 2025 13:47:46 -0500 Subject: [PATCH 2/2] Add support for different configs per property based on the id of the data type associated to the property --- .../ValueConverters/MeganavValueConverter.cs | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs b/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs index 2aa3e82..1d56742 100644 --- a/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs +++ b/src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs @@ -20,7 +20,7 @@ internal class MeganavValueConverter : PropertyValueConverterBase private readonly IUmbracoContextFactory _umbracoContextFactory; private readonly PublishedElementFactory _publishedElementFactory; - private Dictionary _configs; + private Dictionary _configs; public MeganavValueConverter(IPublishedUrlProvider publishedUrlProvider, IVariationContextAccessor variationContextAccessor, IUmbracoContextFactory umbracoContextFactory, PublishedElementFactory publishedElementFactory) { @@ -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)) { @@ -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 BuildItems(IUmbracoContext umbracoContext, IEnumerable entities, int level = 0) + private IEnumerable BuildItems(IUmbracoContext umbracoContext, IEnumerable entities, MeganavConfiguration config, int level = 0) { foreach (var entity in entities) { @@ -83,7 +83,7 @@ private IEnumerable 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) { @@ -134,28 +134,32 @@ private IEnumerable 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) { - var dataTypeAlias = propertyType?.DataType?.EditorAlias ?? string.Empty; - if (string.IsNullOrWhiteSpace(dataTypeAlias)) - dataTypeAlias = "default"; + var dataTypeId = propertyType?.DataType?.Id ?? 0; - if (_configs == null) + _configs ??= new Dictionary(); + + if (!_configs.TryGetValue(dataTypeId, out var config) || config == null) { - _configs = new Dictionary(); + config = propertyType?.DataType?.ConfigurationAs(); + _configs.Add(dataTypeId, config); } - if (_configs.ContainsKey(dataTypeAlias)) { + config ??= new MeganavConfiguration { - _config = propertyType.DataType.ConfigurationAs(); - } + ItemTypes = new List() + }; + + return config; + } } } \ No newline at end of file