9
9
10
10
using Umbraco . Web ;
11
11
using Umbraco . Web . Extensions ;
12
+ using Umbraco . Core . Models . PublishedContent ;
12
13
13
14
public class Link
14
15
{
@@ -42,26 +43,26 @@ public int? Id
42
43
{
43
44
get
44
45
{
45
- if ( _id == null )
46
+ if ( _id == null )
46
47
{
47
48
_id = _linkItem . Value < int ? > ( "id" ) ;
48
- if ( ! _id . HasValue )
49
+ if ( ! _id . HasValue )
49
50
{
50
51
InitPublishedContent ( ) ;
51
52
}
52
53
}
53
54
return _id ;
54
- }
55
+ }
55
56
56
57
}
57
58
58
59
public Udi Udi
59
60
{
60
61
get
61
62
{
62
- if ( _udi == null )
63
+ if ( _udi == null )
63
64
{
64
- if ( ! Udi . TryParse ( _linkItem . Value < string > ( "udi" ) , out _udi ) )
65
+ if ( ! Udi . TryParse ( _linkItem . Value < string > ( "udi" ) , out _udi ) )
65
66
{
66
67
InitPublishedContent ( ) ;
67
68
}
@@ -95,7 +96,7 @@ internal bool Deleted
95
96
else
96
97
{
97
98
_deleted = false ;
98
- }
99
+ }
99
100
}
100
101
return ( bool ) _deleted ;
101
102
}
@@ -125,7 +126,7 @@ public string Target
125
126
}
126
127
}
127
128
128
- public LinkType Type
129
+ public LinkType Type
129
130
{
130
131
get
131
132
{
@@ -163,15 +164,15 @@ private void InitPublishedContent()
163
164
return ;
164
165
}
165
166
166
- var helper = new UmbracoHelper ( UmbracoContext . Current ) ;
167
-
168
167
if ( Udi . TryParse ( _linkItem . Value < string > ( "udi" ) , out _udi ) )
169
168
{
170
169
_content = _udi . ToPublishedContent ( ) ;
171
170
_id = _content ? . Id ;
172
171
}
173
172
else
174
173
{
174
+ var helper = new UmbracoHelper ( UmbracoContext . Current ) ;
175
+
175
176
// there were no Udi so let's try the legacy way
176
177
_id = _linkItem . Value < int ? > ( "id" ) ;
177
178
@@ -182,22 +183,40 @@ private void InitPublishedContent()
182
183
if ( _linkItem . Value < bool > ( "isMedia" ) )
183
184
{
184
185
_content = helper . TypedMedia ( _id . Value ) ;
185
- if ( _content != null )
186
- {
187
- _udi = Udi . Create ( Constants . UdiEntityType . Media , _content . GetKey ( ) ) ;
188
- }
189
186
}
190
187
else
191
188
{
192
189
_content = helper . TypedContent ( _id . Value ) ;
193
- if ( _content != null )
194
- {
195
- _udi = Udi . Create ( Constants . UdiEntityType . Document , _content . GetKey ( ) ) ;
196
- }
197
190
}
191
+ SetUdi ( ) ;
198
192
}
199
193
}
200
194
}
201
195
}
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
+ }
202
221
}
203
222
}
0 commit comments