Skip to content

Commit 4b89725

Browse files
committed
Change how key is resolved from content for legacy data
1 parent 375e5d0 commit 4b89725

File tree

1 file changed

+36
-17
lines changed
  • src/RJP.MultiUrlPicker/Models

1 file changed

+36
-17
lines changed

src/RJP.MultiUrlPicker/Models/Link.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
using Umbraco.Web;
1111
using Umbraco.Web.Extensions;
12+
using Umbraco.Core.Models.PublishedContent;
1213

1314
public class Link
1415
{
@@ -42,26 +43,26 @@ public int? Id
4243
{
4344
get
4445
{
45-
if(_id == null)
46+
if (_id == null)
4647
{
4748
_id = _linkItem.Value<int?>("id");
48-
if(!_id.HasValue)
49+
if (!_id.HasValue)
4950
{
5051
InitPublishedContent();
5152
}
5253
}
5354
return _id;
54-
}
55+
}
5556

5657
}
5758

5859
public Udi Udi
5960
{
6061
get
6162
{
62-
if(_udi == null)
63+
if (_udi == null)
6364
{
64-
if(!Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
65+
if (!Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
6566
{
6667
InitPublishedContent();
6768
}
@@ -95,7 +96,7 @@ internal bool Deleted
9596
else
9697
{
9798
_deleted = false;
98-
}
99+
}
99100
}
100101
return (bool)_deleted;
101102
}
@@ -125,7 +126,7 @@ public string Target
125126
}
126127
}
127128

128-
public LinkType Type
129+
public LinkType Type
129130
{
130131
get
131132
{
@@ -163,15 +164,15 @@ private void InitPublishedContent()
163164
return;
164165
}
165166

166-
var helper = new UmbracoHelper(UmbracoContext.Current);
167-
168167
if (Udi.TryParse(_linkItem.Value<string>("udi"), out _udi))
169168
{
170169
_content = _udi.ToPublishedContent();
171170
_id = _content?.Id;
172171
}
173172
else
174173
{
174+
var helper = new UmbracoHelper(UmbracoContext.Current);
175+
175176
// there were no Udi so let's try the legacy way
176177
_id = _linkItem.Value<int?>("id");
177178

@@ -182,22 +183,40 @@ private void InitPublishedContent()
182183
if (_linkItem.Value<bool>("isMedia"))
183184
{
184185
_content = helper.TypedMedia(_id.Value);
185-
if (_content != null)
186-
{
187-
_udi = Udi.Create(Constants.UdiEntityType.Media, _content.GetKey());
188-
}
189186
}
190187
else
191188
{
192189
_content = helper.TypedContent(_id.Value);
193-
if (_content != null)
194-
{
195-
_udi = Udi.Create(Constants.UdiEntityType.Document, _content.GetKey());
196-
}
197190
}
191+
SetUdi();
198192
}
199193
}
200194
}
201195
}
196+
197+
private void SetUdi()
198+
{
199+
if (_content != null && _udi == null)
200+
{
201+
Guid? key = _content.GetKey();
202+
if (key == Guid.Empty)
203+
{
204+
// if the key is Guid.Empty the model might be created by the ModelsBuilder,
205+
// if so it, by default, derives from PublishedContentModel.
206+
// By calling UnWrap() we get the original content, which probably implements
207+
// IPublishedContentWithKey, so we can get the key
208+
key = (_content as PublishedContentWrapped)?.Unwrap().GetKey();
209+
}
210+
211+
if (key.HasValue && key != Guid.Empty)
212+
{
213+
string udiType = _content.ItemType == PublishedItemType.Media ?
214+
Constants.UdiEntityType.Media :
215+
Constants.UdiEntityType.Document;
216+
217+
_udi = Udi.Create(udiType, key.Value);
218+
}
219+
}
220+
}
202221
}
203222
}

0 commit comments

Comments
 (0)