1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using Newtonsoft . Json ;
5
+ using Newtonsoft . Json . Linq ;
4
6
using Umbraco . Core ;
5
7
using Umbraco . Core . Deploy ;
8
+ using Umbraco . Core . Logging ;
6
9
using Umbraco . Core . Models ;
10
+ using Umbraco . Core . Services ;
7
11
8
12
namespace Umbraco . Deploy . Contrib . Connectors . ValueConnectors
9
13
{
@@ -29,9 +33,17 @@ namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
29
33
/// </summary>
30
34
public class RelatedLinks2ValueConnector : IValueConnector
31
35
{
36
+ private readonly IEntityService _entityService ;
37
+
38
+ public RelatedLinks2ValueConnector ( IEntityService entityService )
39
+ {
40
+ if ( entityService == null ) throw new ArgumentNullException ( nameof ( entityService ) ) ;
41
+ _entityService = entityService ;
42
+ }
43
+
32
44
public string GetValue ( Property property , ICollection < ArtifactDependency > dependencies )
33
45
{
34
-
46
+
35
47
var value = property . Value as string ;
36
48
37
49
if ( string . IsNullOrWhiteSpace ( value ) )
@@ -40,7 +52,44 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
40
52
if ( value . DetectIsJson ( ) == false )
41
53
return null ;
42
54
43
- var relatedLinks = JsonConvert . DeserializeObject < IEnumerable < RelatedLinkUdiModel > > ( value ) ;
55
+ var relatedLinks = new List < RelatedLinkUdiModel > ( ) ;
56
+ try
57
+ {
58
+ relatedLinks = JsonConvert . DeserializeObject < List < RelatedLinkUdiModel > > ( value ) ;
59
+ }
60
+ catch ( JsonSerializationException ex )
61
+ {
62
+ // We might be transferring related links stored as int id, parse with ints instead.
63
+
64
+ var relatedLinksInt = JsonConvert . DeserializeObject < JArray > ( value ) ;
65
+
66
+ if ( relatedLinksInt == null )
67
+ return string . Empty ;
68
+
69
+ foreach ( var relatedLink in relatedLinksInt )
70
+ {
71
+ //Get the value from the JSON object
72
+ var isInternal = Convert . ToBoolean ( relatedLink [ "isInternal" ] ) ;
73
+
74
+ //We are only concerned about internal links
75
+ if ( ! isInternal )
76
+ continue ;
77
+
78
+ var linkIntId = Convert . ToInt32 ( relatedLink [ "link" ] ) ;
79
+
80
+ //get the guid corresponding to the id
81
+ //it *can* fail if eg the id points to a deleted content,
82
+ //and then we use an empty guid
83
+ var guidAttempt = _entityService . GetKeyForId ( linkIntId , UmbracoObjectTypes . Document ) ;
84
+ if ( guidAttempt . Success )
85
+ {
86
+ // replace the picked content id by the corresponding Udi as a dependancy
87
+ var udi = new GuidUdi ( Constants . UdiEntityType . Document , guidAttempt . Result ) ;
88
+
89
+ relatedLinks . Add ( new RelatedLinkUdiModel { Link = udi , IsInternal = true } ) ;
90
+ }
91
+ }
92
+ }
44
93
45
94
if ( relatedLinks == null )
46
95
return null ;
0 commit comments