Skip to content

Commit 2cdbe85

Browse files
Recursively migrate DTGE property values
1 parent 2c03318 commit 2cdbe85

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

src/Umbraco.Deploy.Contrib/Migrators/DocTypeGridEditorPropertyTypeMigrator.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
4+
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Logging;
56
using Newtonsoft.Json;
67
using Newtonsoft.Json.Linq;
78
using Umbraco.Cms.Core;
9+
using Umbraco.Cms.Core.DependencyInjection;
810
using Umbraco.Cms.Core.Deploy;
911
using Umbraco.Cms.Core.Models;
1012
using Umbraco.Cms.Core.Models.Blocks;
1113
using Umbraco.Cms.Core.PropertyEditors;
1214
using Umbraco.Cms.Core.Serialization;
1315
using Umbraco.Cms.Core.Services;
1416
using Umbraco.Cms.Core.Strings;
17+
using Umbraco.Deploy.Core.Migrators;
1518
using Umbraco.Deploy.Infrastructure.Migrators;
1619
using Umbraco.Extensions;
1720

@@ -22,7 +25,10 @@ namespace Umbraco.Deploy.Contrib.Migrators;
2225
/// </summary>
2326
public class DocTypeGridEditorPropertyTypeMigrator : GridPropertyTypeMigrator
2427
{
28+
private readonly ILogger<GridPropertyTypeMigrator> _logger;
2529
private readonly IJsonSerializer _jsonSerializer;
30+
private readonly PropertyTypeMigratorCollection _propertyTypeMigrators;
31+
private IDictionary<string, string>? _propertyEditorAliases;
2632

2733
/// <summary>
2834
/// Initializes a new instance of the <see cref="DocTypeGridEditorPropertyTypeMigrator" /> class.
@@ -33,9 +39,44 @@ public class DocTypeGridEditorPropertyTypeMigrator : GridPropertyTypeMigrator
3339
/// <param name="shortStringHelper">The short string helper.</param>
3440
/// <param name="contentTypeService">The content type service.</param>
3541
/// <param name="mediaService">The media service.</param>
42+
[Obsolete("Use the constructor with all parameters. This will be removed in a future version.")]
3643
public DocTypeGridEditorPropertyTypeMigrator(ILogger<GridPropertyTypeMigrator> logger, IJsonSerializer jsonSerializer, IDataTypeService dataTypeService, IShortStringHelper shortStringHelper, IContentTypeService contentTypeService, IMediaService mediaService)
44+
: this(
45+
logger,
46+
jsonSerializer,
47+
dataTypeService,
48+
shortStringHelper,
49+
contentTypeService,
50+
mediaService,
51+
StaticServiceProvider.Instance.GetRequiredService<PropertyTypeMigratorCollection>())
52+
{ }
53+
54+
/// <summary>
55+
/// Initializes a new instance of the <see cref="DocTypeGridEditorPropertyTypeMigrator" /> class.
56+
/// </summary>
57+
/// <param name="logger">The logger.</param>
58+
/// <param name="jsonSerializer">The JSON serializer.</param>
59+
/// <param name="dataTypeService">The data type service.</param>
60+
/// <param name="shortStringHelper">The short string helper.</param>
61+
/// <param name="contentTypeService">The content type service.</param>
62+
/// <param name="mediaService">The media service.</param>
63+
/// <param name="propertyTypeMigrators">The property type migrators.</param>
64+
public DocTypeGridEditorPropertyTypeMigrator(ILogger<GridPropertyTypeMigrator> logger, IJsonSerializer jsonSerializer, IDataTypeService dataTypeService, IShortStringHelper shortStringHelper, IContentTypeService contentTypeService, IMediaService mediaService, PropertyTypeMigratorCollection propertyTypeMigrators)
3765
: base(logger, jsonSerializer, dataTypeService, shortStringHelper, contentTypeService, mediaService)
38-
=> _jsonSerializer = jsonSerializer;
66+
{
67+
_logger = logger;
68+
_jsonSerializer = jsonSerializer;
69+
_propertyTypeMigrators = propertyTypeMigrators;
70+
}
71+
72+
/// <inheritdoc />
73+
public override object? Migrate(IPropertyType propertyType, object? value, IDictionary<string, string> propertyEditorAliases, IContextCache contextCache)
74+
{
75+
// Workaround: store property editor aliases for use in MigrateGridControl
76+
_propertyEditorAliases = propertyEditorAliases;
77+
78+
return base.Migrate(propertyType, value, propertyEditorAliases, contextCache);
79+
}
3980

4081
/// <inheritdoc />
4182
protected override BlockItemData? MigrateGridControl(GridValue.GridControl gridControl, BlockGridConfiguration configuration, IContextCache contextCache)
@@ -62,11 +103,28 @@ public DocTypeGridEditorPropertyTypeMigrator(ILogger<GridPropertyTypeMigrator> l
62103
IContentType contentType = GetContentType(value.ContentTypeAlias, configuration, contextCache)
63104
?? throw new InvalidOperationException($"Migrating legacy grid failed, because content type with alias '{value.ContentTypeAlias}' could not be found (in the Block Grid configuration).");
64105

106+
var propertyValues = new Dictionary<string, object?>(value.Value.Count);
107+
108+
foreach (IPropertyType propertyType in contentType.CompositionPropertyTypes)
109+
{
110+
if (value.Value.TryGetValue(propertyType.Alias, out object? propertyValue))
111+
{
112+
if (_propertyEditorAliases is not null && _propertyTypeMigrators.TryMigrate(propertyType, propertyValue, _propertyEditorAliases, contentType.Alias, contextCache, out var migratedValue))
113+
{
114+
_logger.LogDebug("Migrated nested/recursive property {PropertyTypeAlias} on {ContentTypeAlias} to {PropertyEditorAlias}: {Value}.", propertyType.Alias, contentType.Alias, propertyType.PropertyEditorAlias, migratedValue);
115+
116+
propertyValue = migratedValue;
117+
}
118+
119+
propertyValues[propertyType.Alias] = propertyValue;
120+
}
121+
}
122+
65123
return new BlockItemData()
66124
{
67125
Udi = Udi.Create(Constants.UdiEntityType.Element, value.Id),
68126
ContentTypeKey = contentType.Key,
69-
RawPropertyValues = value.Value
127+
RawPropertyValues = propertyValues
70128
};
71129
}
72130

0 commit comments

Comments
 (0)