1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics . CodeAnalysis ;
4
+ using Microsoft . Extensions . DependencyInjection ;
4
5
using Microsoft . Extensions . Logging ;
5
6
using Newtonsoft . Json ;
6
7
using Newtonsoft . Json . Linq ;
7
8
using Umbraco . Cms . Core ;
9
+ using Umbraco . Cms . Core . DependencyInjection ;
8
10
using Umbraco . Cms . Core . Deploy ;
9
11
using Umbraco . Cms . Core . Models ;
10
12
using Umbraco . Cms . Core . Models . Blocks ;
11
13
using Umbraco . Cms . Core . PropertyEditors ;
12
14
using Umbraco . Cms . Core . Serialization ;
13
15
using Umbraco . Cms . Core . Services ;
14
16
using Umbraco . Cms . Core . Strings ;
17
+ using Umbraco . Deploy . Core . Migrators ;
15
18
using Umbraco . Deploy . Infrastructure . Migrators ;
16
19
using Umbraco . Extensions ;
17
20
@@ -22,7 +25,10 @@ namespace Umbraco.Deploy.Contrib.Migrators;
22
25
/// </summary>
23
26
public class DocTypeGridEditorPropertyTypeMigrator : GridPropertyTypeMigrator
24
27
{
28
+ private readonly ILogger < GridPropertyTypeMigrator > _logger ;
25
29
private readonly IJsonSerializer _jsonSerializer ;
30
+ private readonly PropertyTypeMigratorCollection _propertyTypeMigrators ;
31
+ private IDictionary < string , string > ? _propertyEditorAliases ;
26
32
27
33
/// <summary>
28
34
/// Initializes a new instance of the <see cref="DocTypeGridEditorPropertyTypeMigrator" /> class.
@@ -33,9 +39,44 @@ public class DocTypeGridEditorPropertyTypeMigrator : GridPropertyTypeMigrator
33
39
/// <param name="shortStringHelper">The short string helper.</param>
34
40
/// <param name="contentTypeService">The content type service.</param>
35
41
/// <param name="mediaService">The media service.</param>
42
+ [ Obsolete ( "Use the constructor with all parameters. This will be removed in a future version." ) ]
36
43
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 )
37
65
: 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
+ }
39
80
40
81
/// <inheritdoc />
41
82
protected override BlockItemData ? MigrateGridControl ( GridValue . GridControl gridControl , BlockGridConfiguration configuration , IContextCache contextCache )
@@ -62,11 +103,28 @@ public DocTypeGridEditorPropertyTypeMigrator(ILogger<GridPropertyTypeMigrator> l
62
103
IContentType contentType = GetContentType ( value . ContentTypeAlias , configuration , contextCache )
63
104
?? throw new InvalidOperationException ( $ "Migrating legacy grid failed, because content type with alias '{ value . ContentTypeAlias } ' could not be found (in the Block Grid configuration).") ;
64
105
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
+
65
123
return new BlockItemData ( )
66
124
{
67
125
Udi = Udi . Create ( Constants . UdiEntityType . Element , value . Id ) ,
68
126
ContentTypeKey = contentType . Key ,
69
- RawPropertyValues = value . Value
127
+ RawPropertyValues = propertyValues
70
128
} ;
71
129
}
72
130
0 commit comments