Skip to content

Commit 645f7c2

Browse files
Support legacy picker in the new picker as well, to make sure people who swap out between legacy/non-legacy datatypes can keep deploying successfully
1 parent 14ebe71 commit 645f7c2

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/RelatedLinks2ValueConnector.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
46
using Umbraco.Core;
57
using Umbraco.Core.Deploy;
8+
using Umbraco.Core.Logging;
69
using Umbraco.Core.Models;
10+
using Umbraco.Core.Services;
711

812
namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
913
{
@@ -29,9 +33,17 @@ namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
2933
/// </summary>
3034
public class RelatedLinks2ValueConnector : IValueConnector
3135
{
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+
3244
public string GetValue(Property property, ICollection<ArtifactDependency> dependencies)
3345
{
34-
46+
3547
var value = property.Value as string;
3648

3749
if (string.IsNullOrWhiteSpace(value))
@@ -40,7 +52,44 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
4052
if (value.DetectIsJson() == false)
4153
return null;
4254

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+
}
4493

4594
if (relatedLinks == null)
4695
return null;

0 commit comments

Comments
 (0)