@@ -18,12 +18,12 @@ public class MeganavValueConnector : IValueConnector
18
18
/// <summary>
19
19
/// The entity service.
20
20
/// </summary>
21
- private readonly IEntityService entityService ;
21
+ private readonly IEntityService _entityService ;
22
22
23
23
/// <summary>
24
24
/// Gets the property editor aliases that the value converter supports by default.
25
25
/// </summary>
26
- public IEnumerable < string > PropertyEditorAliases => new string [ ] { "Cogworks.Meganav" } ;
26
+ public IEnumerable < string > PropertyEditorAliases => new [ ] { "Cogworks.Meganav" } ;
27
27
28
28
/// <summary>
29
29
/// Initializes a new instance of the <see cref="MeganavValueConnector"/> class.
@@ -32,7 +32,7 @@ public class MeganavValueConnector : IValueConnector
32
32
/// <exception cref="ArgumentNullException">entityService</exception>
33
33
public MeganavValueConnector ( IEntityService entityService )
34
34
{
35
- this . entityService = entityService ?? throw new ArgumentNullException ( "entityService" ) ;
35
+ _entityService = entityService ?? throw new ArgumentNullException ( "entityService" ) ;
36
36
}
37
37
38
38
/// <summary>
@@ -52,36 +52,7 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
52
52
}
53
53
54
54
// Parse links and convert ID to UDI
55
- JArray ParseLinks ( JArray links )
56
- {
57
- foreach ( var link in links )
58
- {
59
- GuidUdi guidUdi = null ;
60
- int id = link . Value < int > ( "id" ) ;
61
- if ( id != 0 )
62
- {
63
- Attempt < Guid > keyForId = this . entityService . GetKeyForId ( id , UmbracoObjectTypes . Document ) ;
64
- if ( keyForId . Success )
65
- {
66
- guidUdi = new GuidUdi ( "document" , keyForId . Result ) ;
67
- dependencies . Add ( new ArtifactDependency ( guidUdi , false , ArtifactDependencyMode . Exist ) ) ;
68
- }
69
- }
70
-
71
- link [ "id" ] = guidUdi ? . ToString ( ) ;
72
-
73
- // Parse children
74
- var children = link . Value < JArray > ( "children" ) ;
75
- if ( children != null )
76
- {
77
- link [ "children" ] = ParseLinks ( children ) ;
78
- }
79
- }
80
-
81
- return links ;
82
- }
83
-
84
- var rootLinks = ParseLinks ( JArray . Parse ( value ) ) ;
55
+ var rootLinks = ParseLinks ( JArray . Parse ( value ) , dependencies , Direction . ToArtifact ) ;
85
56
86
57
return rootLinks . ToString ( Formatting . None ) ;
87
58
}
@@ -103,41 +74,58 @@ public void SetValue(IContentBase content, string alias, string value)
103
74
104
75
if ( ! StringExtensions . DetectIsJson ( value ) )
105
76
{
106
- // Skip invalid value (probably shoudn't be saved, right?)
77
+ // Skip invalid values
107
78
return ;
108
79
}
109
80
110
81
// Parse links and convert UDI back to local ID
111
- JArray ParseLinks ( JArray links )
82
+ var rootLinks = ParseLinks ( JArray . Parse ( value ) , null , Direction . FromArtifact ) ;
83
+
84
+ content . SetValue ( alias , rootLinks . ToString ( Formatting . None ) ) ;
85
+ }
86
+
87
+ private JArray ParseLinks ( JArray links , ICollection < ArtifactDependency > dependencies , Direction direction )
88
+ {
89
+ foreach ( var link in links )
112
90
{
113
- foreach ( var link in links )
91
+ if ( direction == Direction . ToArtifact )
114
92
{
115
- int id = 0 ;
93
+ GuidUdi guidUdi = null ;
94
+ var id = link . Value < int > ( "id" ) ;
95
+ if ( id != 0 )
96
+ {
97
+ var keyForId = _entityService . GetKeyForId ( id , UmbracoObjectTypes . Document ) ;
98
+ if ( keyForId . Success )
99
+ {
100
+ guidUdi = new GuidUdi ( Constants . UdiEntityType . Document , keyForId . Result ) ;
101
+ dependencies . Add ( new ArtifactDependency ( guidUdi , false , ArtifactDependencyMode . Exist ) ) ;
102
+ }
103
+ }
104
+ link [ "id" ] = guidUdi ? . ToString ( ) ;
105
+ }
106
+ else
107
+ {
108
+ var id = 0 ;
116
109
if ( GuidUdi . TryParse ( link . Value < string > ( "id" ) , out GuidUdi guidUdi ) )
117
110
{
118
- Attempt < int > idForUdi = this . entityService . GetIdForUdi ( guidUdi ) ;
111
+ var idForUdi = _entityService . GetIdForUdi ( guidUdi ) ;
119
112
if ( idForUdi . Success )
120
113
{
121
114
id = idForUdi . Result ;
122
115
}
123
116
}
124
-
125
117
link [ "id" ] = id ;
126
-
127
- // Parse children
128
- var children = link . Value < JArray > ( "children" ) ;
129
- if ( children != null )
130
- {
131
- link [ "children" ] = ParseLinks ( children ) ;
132
- }
133
118
}
134
119
135
- return links ;
120
+ // Parse children
121
+ var children = link . Value < JArray > ( "children" ) ;
122
+ if ( children != null )
123
+ {
124
+ link [ "children" ] = ParseLinks ( children , dependencies , direction ) ;
125
+ }
136
126
}
137
127
138
- var rootLinks = ParseLinks ( JArray . Parse ( value ) ) ;
139
-
140
- content . SetValue ( alias , rootLinks . ToString ( Formatting . None ) ) ;
128
+ return links ;
141
129
}
142
130
}
143
131
}
0 commit comments