-
Notifications
You must be signed in to change notification settings - Fork 78
V17/nested content #903
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
Merged
Merged
V17/nested content #903
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12b8e8e
Add support for direct migration of nested content to blocklist. (as …
KevinJump 887fbc0
media item - logging parent fail
KevinJump 232337a
Update uSync.Core/Serialization/Serializers/MediaSerializer.cs
KevinJump 39d1f6e
Update uSync.Core/Mapping/Mappers/NestedContentToBlockListHelper.cs
KevinJump 95d7a47
Use OfType instead of Cast when iterating nested content config array…
Copilot 0d21547
Add NUnit migration tests for NestedContentMigratingConfig (#905)
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
uSync.Core/DataTypes/DataTypeSerializers/NestedContentMigratingConfig.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.PropertyEditors; | ||
| using Umbraco.Cms.Core.Services; | ||
|
|
||
| using uSync.Core.Extensions; | ||
|
|
||
| namespace uSync.Core.DataTypes.DataTypeSerializers; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// migrates nested content to a block list element. | ||
| /// </summary> | ||
|
|
||
| internal class NestedContentMigratingConfig : ConfigurationSerializerBase, IConfigurationSerializer | ||
| { | ||
| private readonly IContentTypeService _contentTypeService; | ||
|
|
||
| public NestedContentMigratingConfig(IContentTypeService contentTypeService) | ||
| { | ||
| this._contentTypeService = contentTypeService; | ||
| } | ||
|
|
||
| public string Name => nameof(NestedContentMigratingConfig); | ||
| public string[] Editors => [SyncLegacyTypes.NestedContent, SyncLegacyTypes.OurNestedContent]; | ||
|
|
||
| public string? GetEditorAlias() => Constants.PropertyEditors.Aliases.BlockList; | ||
| public string? GetEditorUIAlias() => "Umb.PropertyEditorUi.BlockList"; | ||
|
|
||
| public override IDictionary<string, object> GetConfigurationImport(IDictionary<string, object> configuration) | ||
| { | ||
| var config = new BlockListConfiguration(); | ||
|
|
||
| if (configuration.TryGetValue("minItems", out var min) && int.TryParse(min?.ToString(), out var minItems)) | ||
| config.ValidationLimit.Min = minItems; | ||
|
|
||
| if (configuration.TryGetValue("maxItems", out var max) && int.TryParse(max?.ToString(), out var maxItems)) | ||
| config.ValidationLimit.Max = maxItems; | ||
|
|
||
| if (configuration.TryGetValue("contentTypes", out var contentTypes) && contentTypes is JsonArray contentTypesArray) | ||
| { | ||
| var blocks = new List<BlockListConfiguration.BlockConfiguration>(); | ||
| foreach (var contentType in contentTypesArray.Cast<JsonObject>()) | ||
| { | ||
| if (contentType["ncAlias"]?.ToString() is not string alias) continue; | ||
| var contentTypeItem = _contentTypeService.Get(alias); | ||
| if (contentTypeItem is null) continue; | ||
| blocks.Add(new BlockListConfiguration.BlockConfiguration | ||
| { | ||
| ContentElementTypeKey = contentTypeItem.Key, | ||
| }); | ||
| } | ||
|
|
||
| config.Blocks = [.. blocks]; | ||
| } | ||
|
|
||
| var result = config.SerializeJsonString().DeserializeJson<Dictionary<string, object>>() ?? configuration; | ||
| return result; | ||
| } | ||
|
KevinJump marked this conversation as resolved.
|
||
| } | ||
83 changes: 83 additions & 0 deletions
83
uSync.Core/Mapping/Mappers/NestedContentToBlockListHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| using Umbraco.Cms.Core.Models.Blocks; | ||
| using Umbraco.Cms.Core.Services; | ||
|
|
||
| using uSync.Core.Extensions; | ||
|
|
||
| namespace uSync.Core.Mapping.Mappers; | ||
|
|
||
| /// <summary> | ||
| /// helper methods that can convert nested content to block list values. | ||
| /// </summary> | ||
| internal class NestedContentToBlockListHelper | ||
| { | ||
| private readonly IContentTypeService _contentTypeService; | ||
|
|
||
| public NestedContentToBlockListHelper(IContentTypeService contentTypeService) | ||
| { | ||
| _contentTypeService = contentTypeService; | ||
| } | ||
|
|
||
| private static string[] _reservedProperties = ["ncContentTypeAlias", "key", "name"]; | ||
|
|
||
| /// <summary> | ||
| /// converts a nested content value to a block list value. This is used when we are converting a nested content property to a block list property. | ||
| /// </summary> | ||
| /// <param name="nestedContentValue">the nested content value to convert</param> | ||
| /// <returns>the converted block list value</returns> | ||
| public string ConvertNestedContentToBlockList(string nestedContentValue) | ||
| { | ||
| if (nestedContentValue.Contains("ncContentTypeAlias") is false) return nestedContentValue; | ||
|
|
||
| var nestedContent = nestedContentValue.DeserializeJson<List<Dictionary<string, object>>>(); | ||
|
KevinJump marked this conversation as resolved.
Outdated
|
||
| if (nestedContent == null) return nestedContentValue; | ||
|
|
||
| BlockListValue blockListValue = new BlockListValue(); | ||
|
|
||
| foreach (var item in nestedContent) | ||
| { | ||
| var contentTypeAlias = item.TryGetValue("ncContentTypeAlias", out var alias) ? alias?.ToString() : null; | ||
| if (contentTypeAlias == null) continue; | ||
|
|
||
| var contentType = _contentTypeService.Get(contentTypeAlias); | ||
| if (contentType == null) continue; | ||
|
|
||
| var blockItemData = new BlockItemData | ||
| { | ||
| ContentTypeKey = contentType.Key, | ||
| Key = item.TryGetValue("key", out var key) && Guid.TryParse(key?.ToString(), out var guidKey) ? guidKey : Guid.NewGuid(), | ||
| }; | ||
|
|
||
| foreach (var value in item.Keys) | ||
| { | ||
| if (_reservedProperties.Contains(value)) continue; | ||
| blockItemData.Values.Add(new BlockPropertyValue | ||
| { | ||
| Alias = value, | ||
| Value = item[value] | ||
| }); | ||
| } | ||
|
|
||
| blockListValue.ContentData.Add(blockItemData); | ||
| } | ||
|
|
||
| blockListValue.Expose = [.. blockListValue.ContentData.Select(x => new BlockItemVariation(x.Key, null, null))]; | ||
| blockListValue.Layout = new Dictionary<string, IEnumerable<IBlockLayoutItem>> | ||
| { | ||
| { | ||
| "Umbraco.BlockList", | ||
| blockListValue.ContentData.Select( | ||
| x => new BlockListLayoutItem | ||
| { | ||
| ContentKey = x.Key, | ||
| SettingsKey = null, | ||
| }) | ||
| } | ||
| }; | ||
|
|
||
| return blockListValue.SerializeJsonString() ?? nestedContentValue; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.