From 292c76df0b6f48335171373e604355774822cb7b Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 31 Jan 2020 15:59:27 +1100 Subject: [PATCH] Getting models and value converter setup along with tests --- src/Umbraco.Core/Constants-PropertyEditors.cs | 5 + .../Models/Blocks/BlockEditorModel.cs | 17 ++- .../Models/Blocks/BlockListLayoutReference.cs | 9 +- .../Models/Blocks/BlockListModel.cs | 16 ++- .../PropertyEditors/BlockListEditor.cs | 12 ++ src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../BlockListPropertyValueConverterTests.cs | 91 +++++++++++++++ .../Published/NestedContentTests.cs | 6 +- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + .../BlockEditorPropertyEditor.cs | 17 +++ .../PropertyEditors/BlockListConfiguration.cs | 24 ++++ .../BlockListPropertyEditor.cs | 21 ++++ .../NestedContentConfiguration.cs | 1 + .../ValueConverters/BlockEditorConverter.cs | 45 ++++++++ .../BlockListPropertyValueConverter.cs | 107 ++++++++++++++++++ .../NestedContentManyValueConverter.cs | 6 +- .../NestedContentSingleValueConverter.cs | 6 +- .../NestedContentValueConverterBase.cs | 34 +----- src/Umbraco.Web/Umbraco.Web.csproj | 5 + 19 files changed, 382 insertions(+), 42 deletions(-) create mode 100644 src/Umbraco.Core/PropertyEditors/BlockListEditor.cs create mode 100644 src/Umbraco.Tests/PropertyEditors/BlockListPropertyValueConverterTests.cs create mode 100644 src/Umbraco.Web/PropertyEditors/BlockEditorPropertyEditor.cs create mode 100644 src/Umbraco.Web/PropertyEditors/BlockListConfiguration.cs create mode 100644 src/Umbraco.Web/PropertyEditors/BlockListPropertyEditor.cs create mode 100644 src/Umbraco.Web/PropertyEditors/ValueConverters/BlockEditorConverter.cs create mode 100644 src/Umbraco.Web/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs diff --git a/src/Umbraco.Core/Constants-PropertyEditors.cs b/src/Umbraco.Core/Constants-PropertyEditors.cs index eb2b3525a7..753cd72116 100644 --- a/src/Umbraco.Core/Constants-PropertyEditors.cs +++ b/src/Umbraco.Core/Constants-PropertyEditors.cs @@ -36,6 +36,11 @@ namespace Umbraco.Core /// public static class Aliases { + /// + /// CheckBox List. + /// + public const string BlockList = "Umbraco.BlockList"; + /// /// CheckBox List. /// diff --git a/src/Umbraco.Core/Models/Blocks/BlockEditorModel.cs b/src/Umbraco.Core/Models/Blocks/BlockEditorModel.cs index 7d00623ccf..307b69f6e0 100644 --- a/src/Umbraco.Core/Models/Blocks/BlockEditorModel.cs +++ b/src/Umbraco.Core/Models/Blocks/BlockEditorModel.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Core.Models.Blocks @@ -8,9 +10,20 @@ namespace Umbraco.Core.Models.Blocks /// public abstract class BlockEditorModel { + protected BlockEditorModel(IEnumerable data) + { + Data = data ?? throw new ArgumentNullException(nameof(data)); + } + + public BlockEditorModel() + { + } + + /// /// The data items of the Block List editor /// - public IEnumerable Data { get; } + [DataMember(Name = "data")] + public IEnumerable Data { get; set; } } } diff --git a/src/Umbraco.Core/Models/Blocks/BlockListLayoutReference.cs b/src/Umbraco.Core/Models/Blocks/BlockListLayoutReference.cs index 444bb7249f..163b564c9f 100644 --- a/src/Umbraco.Core/Models/Blocks/BlockListLayoutReference.cs +++ b/src/Umbraco.Core/Models/Blocks/BlockListLayoutReference.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.Serialization; using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Core.Models.Blocks @@ -6,6 +7,7 @@ namespace Umbraco.Core.Models.Blocks /// /// Represents a layout item for the Block List editor /// + [DataContract(Name = "blockListLayout", Namespace = "")] public class BlockListLayoutReference : IBlockElement { public BlockListLayoutReference(Udi udi, IPublishedElement settings) @@ -14,7 +16,10 @@ namespace Umbraco.Core.Models.Blocks Settings = settings ?? throw new ArgumentNullException(nameof(settings)); } - public Udi Udi { get; } - public IPublishedElement Settings { get; } + [DataMember(Name = "udi")] + public Udi Udi { get; set; } + + [DataMember(Name = "settings")] + public IPublishedElement Settings { get; set; } } } diff --git a/src/Umbraco.Core/Models/Blocks/BlockListModel.cs b/src/Umbraco.Core/Models/Blocks/BlockListModel.cs index 5331ca3891..36dd8af7c1 100644 --- a/src/Umbraco.Core/Models/Blocks/BlockListModel.cs +++ b/src/Umbraco.Core/Models/Blocks/BlockListModel.cs @@ -1,16 +1,30 @@ using System.Collections.Generic; +using System.Runtime.Serialization; +using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Core.Models.Blocks { /// /// The strongly typed model for the Block List editor /// + [DataContract(Name = "blockList", Namespace = "")] public class BlockListModel : BlockEditorModel { + public BlockListModel(IEnumerable data, IEnumerable layout) + : base(data) + { + Layout = layout; + } + + public BlockListModel() + { + } + /// /// The layout items of the Block List editor /// - public IEnumerable Layout { get; } + [DataMember(Name = "layout")] + public IEnumerable Layout { get; set; } } diff --git a/src/Umbraco.Core/PropertyEditors/BlockListEditor.cs b/src/Umbraco.Core/PropertyEditors/BlockListEditor.cs new file mode 100644 index 0000000000..16c6176624 --- /dev/null +++ b/src/Umbraco.Core/PropertyEditors/BlockListEditor.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Umbraco.Core.PropertyEditors +{ + public class BlockListEditor + { + } +} diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 8bf0b9105f..33796a93bf 100755 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -133,6 +133,7 @@ + diff --git a/src/Umbraco.Tests/PropertyEditors/BlockListPropertyValueConverterTests.cs b/src/Umbraco.Tests/PropertyEditors/BlockListPropertyValueConverterTests.cs new file mode 100644 index 0000000000..f345bf136c --- /dev/null +++ b/src/Umbraco.Tests/PropertyEditors/BlockListPropertyValueConverterTests.cs @@ -0,0 +1,91 @@ +using Moq; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Umbraco.Core; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Web.PropertyEditors; +using Umbraco.Web.PropertyEditors.ValueConverters; +using Umbraco.Web.PublishedCache; + +namespace Umbraco.Tests.PropertyEditors +{ + [TestFixture] + public class BlockListPropertyValueConverterTests + { + private BlockListPropertyValueConverter Create() + { + var publishedSnapshotAccessor = Mock.Of(); + var publishedModelFactory = Mock.Of(); + var editor = new BlockListPropertyValueConverter( + Mock.Of(), + publishedModelFactory, + new BlockEditorConverter(publishedSnapshotAccessor, publishedModelFactory)); + return editor; + } + + [Test] + public void Is_Converter_For() + { + var editor = Create(); + Assert.IsTrue(editor.IsConverter(Mock.Of(x => x.EditorAlias == Constants.PropertyEditors.Aliases.BlockList))); + Assert.IsFalse(editor.IsConverter(Mock.Of(x => x.EditorAlias == Constants.PropertyEditors.Aliases.NestedContent))); + } + + [Test] + public void Get_Value_Type_Multiple() + { + var editor = Create(); + var config = new BlockListConfiguration + { + ElementTypes = new[] { + new BlockListConfiguration.ElementType + { + Alias = "Test1" + }, + new BlockListConfiguration.ElementType + { + Alias = "Test2" + } + } + }; + + var dataType = new PublishedDataType(1, "test", new Lazy(() => config)); + var propType = Mock.Of(x => x.DataType == dataType); + + var valueType = editor.GetPropertyValueType(propType); + + Assert.AreEqual(typeof(IEnumerable), valueType); + } + + [Test] + public void Get_Value_Type_Single() + { + var editor = Create(); + var config = new BlockListConfiguration + { + ElementTypes = new[] { + new BlockListConfiguration.ElementType + { + Alias = "Test1" + } + } + }; + + var dataType = new PublishedDataType(1, "test", new Lazy(() => config)); + var propType = Mock.Of(x => x.DataType == dataType); + + var valueType = editor.GetPropertyValueType(propType); + + var modelType = typeof(IEnumerable<>).MakeGenericType(ModelType.For(config.ElementTypes[0].Alias)); + + // we can't compare the exact match of types because ModelType.For generates a new/different type even if the same alias is used + Assert.AreEqual(modelType.FullName, valueType.FullName); + } + + + } +} diff --git a/src/Umbraco.Tests/Published/NestedContentTests.cs b/src/Umbraco.Tests/Published/NestedContentTests.cs index adfb9d3b6b..a102b9f93e 100644 --- a/src/Umbraco.Tests/Published/NestedContentTests.cs +++ b/src/Umbraco.Tests/Published/NestedContentTests.cs @@ -119,10 +119,12 @@ namespace Umbraco.Tests.Published .Setup(x => x.PublishedSnapshot) .Returns(publishedSnapshot.Object); + var blockEditorConverter = new BlockEditorConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object); + var converters = new PropertyValueConverterCollection(new IPropertyValueConverter[] { - new NestedContentSingleValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog), - new NestedContentManyValueConverter(publishedSnapshotAccessor.Object, publishedModelFactory.Object, proflog), + new NestedContentSingleValueConverter(blockEditorConverter, publishedModelFactory.Object, proflog), + new NestedContentManyValueConverter(blockEditorConverter, publishedModelFactory.Object, proflog), }); var factory = new PublishedContentTypeFactory(publishedModelFactory.Object, converters, dataTypeService); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index ff923bb04b..2d980461d2 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -145,6 +145,7 @@ + diff --git a/src/Umbraco.Web/PropertyEditors/BlockEditorPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/BlockEditorPropertyEditor.cs new file mode 100644 index 0000000000..b9ee1b84fb --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/BlockEditorPropertyEditor.cs @@ -0,0 +1,17 @@ +using Umbraco.Core.Logging; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors +{ + /// + /// Abstract class for block editor based editors + /// + public abstract class BlockEditorPropertyEditor : DataEditor + { + public const string ContentTypeAliasPropertyKey = "contentTypeAlias"; + + public BlockEditorPropertyEditor(ILogger logger) : base(logger) + { + } + } +} diff --git a/src/Umbraco.Web/PropertyEditors/BlockListConfiguration.cs b/src/Umbraco.Web/PropertyEditors/BlockListConfiguration.cs new file mode 100644 index 0000000000..95366db486 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/BlockListConfiguration.cs @@ -0,0 +1,24 @@ +using Newtonsoft.Json; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors +{ + + /// + /// The configuration object for the Block List editor + /// + public class BlockListConfiguration + { + [ConfigurationField("elementTypes", "Element Types", "views/propertyeditors/blocklist/blocklist.elementtypepicker.html", Description = "Select the Element Types to use as models for the items.")] + public ElementType[] ElementTypes { get; set; } + + // TODO: Fill me in + + public class ElementType + { + [JsonProperty("elementTypeAlias")] + public string Alias { get; set; } + } + + } +} diff --git a/src/Umbraco.Web/PropertyEditors/BlockListPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/BlockListPropertyEditor.cs new file mode 100644 index 0000000000..f0148f7a9a --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/BlockListPropertyEditor.cs @@ -0,0 +1,21 @@ +using Umbraco.Core; +using Umbraco.Core.Logging; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; + +namespace Umbraco.Web.PropertyEditors +{ + + [DataEditor( + Constants.PropertyEditors.Aliases.BlockList, + "Block List", + "blocklist", + Icon = "icon-list", + Group = Constants.PropertyEditors.Groups.Lists)] + public class BlockListPropertyEditor : BlockEditorPropertyEditor + { + public BlockListPropertyEditor(ILogger logger) : base(logger) + { + } + } +} diff --git a/src/Umbraco.Web/PropertyEditors/NestedContentConfiguration.cs b/src/Umbraco.Web/PropertyEditors/NestedContentConfiguration.cs index 0f53207462..89190883c8 100644 --- a/src/Umbraco.Web/PropertyEditors/NestedContentConfiguration.cs +++ b/src/Umbraco.Web/PropertyEditors/NestedContentConfiguration.cs @@ -3,6 +3,7 @@ using Umbraco.Core.PropertyEditors; namespace Umbraco.Web.PropertyEditors { + /// /// Represents the configuration for the nested content value editor. /// diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockEditorConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockEditorConverter.cs new file mode 100644 index 0000000000..0ab9b86572 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockEditorConverter.cs @@ -0,0 +1,45 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Web.PublishedCache; + +namespace Umbraco.Web.PropertyEditors.ValueConverters +{ + public sealed class BlockEditorConverter + { + private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor; + private readonly IPublishedModelFactory _publishedModelFactory; + + public BlockEditorConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor, IPublishedModelFactory publishedModelFactory) + { + _publishedSnapshotAccessor = publishedSnapshotAccessor; + _publishedModelFactory = publishedModelFactory; + } + + public IPublishedElement ConvertToElement( + JObject sourceObject, string contentTypeAliasPropertyKey, + PropertyCacheLevel referenceCacheLevel, bool preview) + { + var elementTypeAlias = sourceObject[contentTypeAliasPropertyKey]?.ToObject(); + if (string.IsNullOrEmpty(elementTypeAlias)) + return null; + + // only convert element types - content types will cause an exception when PublishedModelFactory creates the model + var publishedContentType = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetContentType(elementTypeAlias); + if (publishedContentType == null || publishedContentType.IsElement == false) + return null; + + var propertyValues = sourceObject.ToObject>(); + + if (!propertyValues.TryGetValue("key", out var keyo) + || !Guid.TryParse(keyo.ToString(), out var key)) + key = Guid.Empty; + + IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); + element = _publishedModelFactory.CreateModel(element); + return element; + } + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs new file mode 100644 index 0000000000..dceaa9995e --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs @@ -0,0 +1,107 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using Umbraco.Core; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.Blocks; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.PropertyEditors.ValueConverters; + +namespace Umbraco.Web.PropertyEditors.ValueConverters +{ + + [DefaultPropertyValueConverter(typeof(JsonValueConverter))] + public class BlockListPropertyValueConverter : PropertyValueConverterBase + { + private readonly IProfilingLogger _proflog; + private readonly IPublishedModelFactory _publishedModelFactory; + private readonly BlockEditorConverter _blockConverter; + + public BlockListPropertyValueConverter(IProfilingLogger proflog, IPublishedModelFactory publishedModelFactory, BlockEditorConverter blockConverter) + { + _proflog = proflog; + _publishedModelFactory = publishedModelFactory; + _blockConverter = blockConverter; + } + + /// + public override bool IsConverter(IPublishedPropertyType propertyType) + => propertyType.EditorAlias.InvariantEquals(Constants.PropertyEditors.Aliases.BlockList); + + /// + public override Type GetPropertyValueType(IPublishedPropertyType propertyType) + { + var contentTypes = propertyType.DataType.ConfigurationAs().ElementTypes; + return contentTypes.Length == 1 + ? typeof(IEnumerable<>).MakeGenericType(ModelType.For(contentTypes[0].Alias)) + : typeof(IEnumerable); + } + + /// + public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) + => PropertyCacheLevel.Element; + + /// + public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) + { + return source?.ToString(); + } + + /// + public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) + { + using (_proflog.DebugDuration($"ConvertPropertyToBlockList ({propertyType.DataType.Id})")) + { + var configuration = propertyType.DataType.ConfigurationAs(); + var contentTypes = configuration.ElementTypes; + var elements = contentTypes.Length == 1 + ? (IList)_publishedModelFactory.CreateModelList(contentTypes[0].Alias) + : new List(); + + var layout = new List(); + var model = new BlockListModel(elements, layout); + + var value = (string)inter; + if (string.IsNullOrWhiteSpace(value)) return model; + + var objects = JsonConvert.DeserializeObject(value); + if (objects.Count == 0) return model; + + var jsonLayout = objects["layout"] as JObject; + if (jsonLayout == null) return model; + + var jsonData = objects["data"] as JArray; + if (jsonData == null) return model; + + var blockListLayouts = jsonLayout[Constants.PropertyEditors.Aliases.BlockList] as JArray; + if (blockListLayouts == null) return model; + + foreach(var blockListLayout in blockListLayouts) + { + var settingsJson = blockListLayout["settings"] as JObject; + if (settingsJson == null) continue; + + var element = _blockConverter.ConvertToElement(settingsJson, BlockEditorPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview); + if (element == null) continue; + + var layoutRef = new BlockListLayoutReference(blockListLayout.Value("udi"), element); + layout.Add(layoutRef); + } + + foreach (var data in jsonData.Cast()) + { + var element = _blockConverter.ConvertToElement(data, BlockEditorPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview); + if (element == null) continue; + elements.Add(element); + } + + return model; + } + } + + + } +} diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs index 4a25049695..b961048851 100644 --- a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentManyValueConverter.cs @@ -23,8 +23,8 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters /// /// Initializes a new instance of the class. /// - public NestedContentManyValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor, IPublishedModelFactory publishedModelFactory, IProfilingLogger proflog) - : base(publishedSnapshotAccessor, publishedModelFactory) + public NestedContentManyValueConverter(BlockEditorConverter blockEditorConverter, IPublishedModelFactory publishedModelFactory, IProfilingLogger proflog) + : base(blockEditorConverter, publishedModelFactory) { _proflog = proflog; } @@ -71,7 +71,7 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters foreach (var sourceObject in objects) { - var element = ConvertToElement(sourceObject, referenceCacheLevel, preview); + var element = BlockEditorConverter.ConvertToElement(sourceObject, NestedContentPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview); if (element != null) elements.Add(element); } diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs index c9c99615f6..b3a2a9294d 100644 --- a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs @@ -22,8 +22,8 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters /// /// Initializes a new instance of the class. /// - public NestedContentSingleValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor, IPublishedModelFactory publishedModelFactory, IProfilingLogger proflog) - : base(publishedSnapshotAccessor, publishedModelFactory) + public NestedContentSingleValueConverter(BlockEditorConverter blockEditorConverter, IPublishedModelFactory publishedModelFactory, IProfilingLogger proflog) + : base(blockEditorConverter, publishedModelFactory) { _proflog = proflog; } @@ -65,7 +65,7 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters if (objects.Count > 1) throw new InvalidOperationException(); - return ConvertToElement(objects[0], referenceCacheLevel, preview); + return BlockEditorConverter.ConvertToElement(objects[0], NestedContentPropertyEditor.ContentTypeAliasPropertyKey, referenceCacheLevel, preview); } } } diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs index 7c18d8ebca..4295daf5fe 100644 --- a/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentValueConverterBase.cs @@ -1,23 +1,19 @@ -using System; -using System.Collections.Generic; -using Newtonsoft.Json.Linq; -using Umbraco.Core; +using Umbraco.Core; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.PropertyEditors; -using Umbraco.Web.PublishedCache; namespace Umbraco.Web.PropertyEditors.ValueConverters { public abstract class NestedContentValueConverterBase : PropertyValueConverterBase { - private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor; - protected NestedContentValueConverterBase(IPublishedSnapshotAccessor publishedSnapshotAccessor, IPublishedModelFactory publishedModelFactory) + protected NestedContentValueConverterBase(BlockEditorConverter blockEditorConverter, IPublishedModelFactory publishedModelFactory) { - _publishedSnapshotAccessor = publishedSnapshotAccessor; + BlockEditorConverter = blockEditorConverter; PublishedModelFactory = publishedModelFactory; } + protected BlockEditorConverter BlockEditorConverter { get; } protected IPublishedModelFactory PublishedModelFactory { get; } public static bool IsNested(IPublishedPropertyType publishedProperty) @@ -39,26 +35,6 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters return IsNested(publishedProperty) && !IsNestedSingle(publishedProperty); } - protected IPublishedElement ConvertToElement(JObject sourceObject, PropertyCacheLevel referenceCacheLevel, bool preview) - { - var elementTypeAlias = sourceObject[NestedContentPropertyEditor.ContentTypeAliasPropertyKey]?.ToObject(); - if (string.IsNullOrEmpty(elementTypeAlias)) - return null; - - // only convert element types - content types will cause an exception when PublishedModelFactory creates the model - var publishedContentType = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetContentType(elementTypeAlias); - if (publishedContentType == null || publishedContentType.IsElement == false) - return null; - - var propertyValues = sourceObject.ToObject>(); - - if (!propertyValues.TryGetValue("key", out var keyo) - || !Guid.TryParse(keyo.ToString(), out var key)) - key = Guid.Empty; - - IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); - element = PublishedModelFactory.CreateModel(element); - return element; - } + } } diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 0111a36993..e660108cad 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -233,8 +233,13 @@ + + + + +