Skip to content

Commit dd8b451

Browse files
Add MediaWithCrops<T> to get generic type access to the media item
1 parent 3d75e39 commit dd8b451

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/Umbraco.Core/Models/MediaWithCrops.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,73 @@
44
namespace Umbraco.Core.Models
55
{
66
/// <summary>
7-
/// Model used in Razor Views for rendering
7+
/// Represents a media item with local crops.
88
/// </summary>
9+
/// <seealso cref="Umbraco.Core.Models.PublishedContent.PublishedContentWrapped" />
910
public class MediaWithCrops : PublishedContentWrapped
1011
{
12+
/// <summary>
13+
/// Gets the media item.
14+
/// </summary>
15+
/// <value>
16+
/// The media item.
17+
/// </value>
1118
public IPublishedContent MediaItem => Unwrap();
1219

20+
/// <summary>
21+
/// Gets the local crops.
22+
/// </summary>
23+
/// <value>
24+
/// The local crops.
25+
/// </value>
1326
public ImageCropperValue LocalCrops { get; }
1427

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="MediaWithCrops" /> class.
30+
/// </summary>
31+
/// <param name="content">The content.</param>
32+
/// <param name="localCrops">The local crops.</param>
1533
public MediaWithCrops(IPublishedContent content, ImageCropperValue localCrops)
1634
: base(content)
1735
{
1836
LocalCrops = localCrops;
1937
}
2038
}
39+
40+
/// <summary>
41+
/// Represents a media item with local crops.
42+
/// </summary>
43+
/// <typeparam name="T">The type of the media item.</typeparam>
44+
/// <seealso cref="Umbraco.Core.Models.PublishedContent.PublishedContentWrapped" />
45+
public class MediaWithCrops<T> : MediaWithCrops
46+
where T : IPublishedContent
47+
{
48+
/// <summary>
49+
/// Gets the media item.
50+
/// </summary>
51+
/// <value>
52+
/// The media item.
53+
/// </value>
54+
public new T MediaItem { get; }
55+
56+
/// <summary>
57+
/// Initializes a new instance of the <see cref="MediaWithCrops{T}" /> class.
58+
/// </summary>
59+
/// <param name="content">The content.</param>
60+
/// <param name="localCrops">The local crops.</param>
61+
public MediaWithCrops(T content, ImageCropperValue localCrops)
62+
: base(content, localCrops)
63+
{
64+
MediaItem = content;
65+
}
66+
67+
/// <summary>
68+
/// Performs an implicit conversion from <see cref="MediaWithCrops{T}" /> to <see cref="T" />.
69+
/// </summary>
70+
/// <param name="mediaWithCrops">The media with crops.</param>
71+
/// <returns>
72+
/// The result of the conversion.
73+
/// </returns>
74+
public static implicit operator T(MediaWithCrops<T> mediaWithCrops) => mediaWithCrops.MediaItem;
75+
}
2176
}

src/Umbraco.Web/PropertyEditors/ValueConverters/BlockListPropertyValueConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub
120120
settingsData = null;
121121
}
122122

123+
// TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow
123124
var layoutType = typeof(BlockListItem<,>).MakeGenericType(contentData.GetType(), settingsData?.GetType() ?? typeof(IPublishedElement));
124125
var layoutRef = (BlockListItem)Activator.CreateInstance(layoutType, contentGuidUdi, contentData, settingGuidUdi, settingsData);
125126

src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub
6767

6868
localCrops.ApplyConfiguration(configuration);
6969

70-
mediaItems.Add(new MediaWithCrops(mediaItem, localCrops));
70+
// TODO: This should be optimized/cached, as calling Activator.CreateInstance is slow
71+
var mediaWithCropsType = typeof(MediaWithCrops<>).MakeGenericType(mediaItem.GetType());
72+
var mediaWithCrops = (MediaWithCrops)Activator.CreateInstance(mediaWithCropsType, mediaItem, localCrops);
73+
74+
mediaItems.Add(mediaWithCrops);
7175

7276
if (!isMultiple)
7377
{

0 commit comments

Comments
 (0)