1
- using Newtonsoft . Json ;
2
- using Newtonsoft . Json . Linq ;
1
+ using Newtonsoft . Json ;
3
2
using System ;
4
3
using System . Collections . Generic ;
5
4
using System . Linq ;
10
9
using Umbraco . Cms . Core . Services ;
11
10
using Umbraco . Cms . Infrastructure . Serialization ;
12
11
using Umbraco . Deploy . Core . Connectors . ValueConnectors . Services ;
13
- using Umbraco . Deploy . Contrib . ValueConnectors ;
14
- using Umbraco . Extensions ;
15
12
using Microsoft . Extensions . Logging ;
13
+ using Umbraco . Deploy . Infrastructure . Connectors . ValueConnectors ;
14
+ using Umbraco . Cms . Core . Models . Blocks ;
15
+ using Umbraco . Deploy . Core . Migrators ;
16
+ using Umbraco . Deploy . Infrastructure . Extensions ;
16
17
17
18
namespace Umbraco . Commerce . Deploy . Connectors . ValueConnectors
18
19
{
19
20
/// <summary>
20
21
/// A Deploy connector for the Umbraco Commerce Variants Editor property editor
21
22
/// </summary>
22
- public class UmbracoCommerceVariantsEditorValueConnector : BlockEditorValueConnector , IValueConnector2
23
+ public class UmbracoCommerceVariantsEditorValueConnector : BlockValueConnectorBase , IValueConnector
23
24
{
24
25
private readonly IUmbracoCommerceApi _umbracoCommerceApi ;
25
26
26
27
public override IEnumerable < string > PropertyEditorAliases => new [ ] { "Umbraco.Commerce.VariantsEditor" } ;
27
28
28
- public UmbracoCommerceVariantsEditorValueConnector ( IUmbracoCommerceApi umbracoCommerceApi ,
29
- IContentTypeService contentTypeService ,
29
+ public UmbracoCommerceVariantsEditorValueConnector (
30
+ IUmbracoCommerceApi umbracoCommerceApi ,
31
+ IContentTypeService contentTypeService ,
30
32
Lazy < ValueConnectorCollection > valueConnectors ,
33
+ PropertyTypeMigratorCollection propertyTypeMigrators ,
31
34
ILogger < UmbracoCommerceVariantsEditorValueConnector > logger )
32
- : base ( contentTypeService , valueConnectors , logger )
35
+ : base ( contentTypeService , valueConnectors , propertyTypeMigrators , logger )
33
36
{
34
37
_umbracoCommerceApi = umbracoCommerceApi ;
35
38
}
36
39
37
40
public override string ToArtifact ( object value , IPropertyType propertyType , ICollection < ArtifactDependency > dependencies , IContextCache contextCache )
38
41
{
39
- var artifact = base . ToArtifact ( value , propertyType , dependencies , contextCache ) ;
40
-
41
- if ( string . IsNullOrWhiteSpace ( artifact ) || ! artifact . DetectIsJson ( ) )
42
- return null ;
43
-
44
- // The base call to ToArtifact will have stripped off the storeId property
45
- // held in the root of the property value so we need to re-parse the original
46
- // value and extract the store ID. If one is present, then we need to append
47
- // this back into the artifact value but also then attempt to process any
48
- // product attributes that need deploying.
49
-
50
- var originalVal = value is JObject
51
- ? value . ToString ( )
52
- : value as string ;
53
-
54
- var baseValue = JsonConvert . DeserializeObject < BaseValue > ( originalVal ) ;
55
- if ( baseValue != null && baseValue . StoreId . HasValue )
42
+ if ( value is string input && input . TryParseJson ( out VariantsBlockEditorValue result ) && result != null )
56
43
{
57
- var blockEditorValue = JsonConvert . DeserializeObject < VariantsBlockEditorValue > ( artifact ) ;
58
- if ( blockEditorValue == null )
59
- return null ;
60
-
61
- blockEditorValue . StoreId = baseValue . StoreId . Value ;
44
+ // Recursive
45
+ result . Content = ToArtifact ( result . Content , dependencies , contextCache ) . ToList ( ) ;
46
+ result . Settings = ToArtifact ( result . Settings , dependencies , contextCache ) . ToList ( ) ;
62
47
63
- var productAttributeAliases = blockEditorValue . Layout . Items . SelectMany ( x => x . Config . Attributes . Keys )
64
- . Distinct ( ) ;
48
+ var productAttributeAliases = result . Layout . Items . SelectMany ( x => x . Config . Attributes . Keys )
49
+ . Distinct ( ) ;
65
50
66
51
foreach ( var productAttributeAlias in productAttributeAliases )
67
52
{
68
- var productAttribute = _umbracoCommerceApi . GetProductAttribute ( blockEditorValue . StoreId . Value , productAttributeAlias ) ;
53
+ var productAttribute = _umbracoCommerceApi . GetProductAttribute ( result . StoreId . Value , productAttributeAlias ) ;
69
54
if ( productAttribute != null )
70
55
{
71
56
dependencies . Add ( new UmbracoCommerceArtifactDependency ( productAttribute . GetUdi ( ) ) ) ;
72
57
}
73
58
}
74
59
75
- artifact = JsonConvert . SerializeObject ( blockEditorValue ) ;
60
+ return JsonConvert . SerializeObject ( result , Formatting . None ) ;
76
61
}
77
62
78
- return artifact ;
63
+ return null ;
79
64
}
80
65
81
- public override object FromArtifact ( string value , IPropertyType propertyType , object currentValue , IContextCache contextCache )
66
+ public override object FromArtifact ( string value , IPropertyType propertyType , object currentValue , IDictionary < string , string > propertyEditorAliases , IContextCache contextCache )
82
67
{
83
- var entity = base . FromArtifact ( value , propertyType , currentValue , contextCache ) ;
84
-
85
- var jObj = entity as JObject ;
86
- if ( jObj != null && ! string . IsNullOrWhiteSpace ( value ) && value . DetectIsJson ( ) )
68
+ if ( value is string input && input . TryParseJson ( out VariantsBlockEditorValue result ) && result != null )
87
69
{
88
- var baseValue = JsonConvert . DeserializeObject < BaseValue > ( value ) ;
89
- if ( baseValue != null && baseValue . StoreId . HasValue )
90
- {
91
- jObj [ "storeId" ] = baseValue . StoreId . Value ;
92
- }
70
+ // Recursive
71
+ result . Content = FromArtifact ( result . Content , propertyEditorAliases , contextCache ) . ToList ( ) ;
72
+ result . Settings = FromArtifact ( result . Settings , propertyEditorAliases , contextCache ) . ToList ( ) ;
73
+
74
+ return JsonConvert . SerializeObject ( result , Formatting . None ) ;
93
75
}
94
76
95
- return jObj ?? entity ;
77
+ return null ;
96
78
}
97
79
98
- object IValueConnector2 . FromArtifact ( string value , IPropertyType propertyType , object currentValue , IContextCache contextCache )
80
+ object IValueConnector . FromArtifact ( string value , IPropertyType propertyType , object currentValue , IContextCache contextCache )
99
81
=> FromArtifact ( value , propertyType , currentValue , contextCache ) ;
100
82
101
- string IValueConnector2 . ToArtifact ( object value , IPropertyType propertyType , ICollection < ArtifactDependency > dependencies , IContextCache contextCache )
83
+ string IValueConnector . ToArtifact ( object value , IPropertyType propertyType , ICollection < ArtifactDependency > dependencies , IContextCache contextCache )
102
84
=> ToArtifact ( value , propertyType , dependencies , contextCache ) ;
103
85
104
86
public class BaseValue
@@ -113,10 +95,10 @@ public class VariantsBlockEditorValue : BaseValue
113
95
public VariantsBlockEditorLayout Layout { get ; set ; }
114
96
115
97
[ JsonProperty ( "contentData" ) ]
116
- public IEnumerable < Block > Content { get ; set ; }
98
+ public IEnumerable < BlockItemData > Content { get ; set ; }
117
99
118
100
[ JsonProperty ( "settingsData" ) ]
119
- public IEnumerable < Block > Settings { get ; set ; }
101
+ public IEnumerable < BlockItemData > Settings { get ; set ; }
120
102
}
121
103
122
104
public class VariantsBlockEditorLayout
0 commit comments