Multiple Media Picker returns list of IPublishedElement
See original GitHub issueNote: Umbraco HQ has marked this as breaking. Some more info is available below:
Unfortunately fixing this issue led to an unintended breaking change, as you can read below, you will run into this is you are currently querying items picked in a media picker in a strongly typed way, like:
@if (post.PreviewImage != null)
{
<img src="@post.PreviewImage.GetCropUrl("Blog Preview")" alt="@post.PreviewImage.AltDescription">
}
altDescription
in this case is a property on you media type, it could also be the default umbracoWidth
property for example.
To fix this, you can update your view in a few different ways:
- Use the returned
IPublishedContent
directly and get theValue
for the property like you are used to from other pickers:
@if (post.PreviewImage != null)
{
<img src="@post.PreviewImage.GetCropUrl("Blog Preview")" alt="@post.PreviewImage.Value("altDescription")">
}
- If you enjoy working with strongly typed objects you can cast the item you’re working with to the type you know it should be, for example:
@if (post.PreviewImage is Image previewImage)
{
<img src="@previewImage.GetCropUrl("Blog Preview")" alt="@previewImage.AltDescription">
}
- For you ModelsBuilder lovers, Stéphane has described some additional options here: https://www.zpqrtbnk.net/posts/umbraco-8-1-4-breaking-models-builder/
The original issue description follows:
Hi
When creating a Media Picker data type and setting both “Pick multiple items” and “Pick only images” to true, then it consistently returns IEnumerable<IPublishedElement>
instead of IEnumerable<IPublishedContent>
.
So if we do a similar setup, like the one in the documentation (https://our.umbraco.com/documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker/) and try to retrieve the value as mentioned in the documentation too, then it will throw an null error.
@{
var typedMultiMediaPicker = Model.Value<IEnumerable<IPublishedContent>>("sliders");
foreach (var item in typedMultiMediaPicker)
{
<img src="@item.Url" style="width:200px"/>
}
}
It works just fine in all other combinations, except when the two checkboxes are both set to true.
I have experienced this bug on every Umbraco 8 versions so far.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (10 by maintainers)
The following custom PVC makes sure the returned type is set back to
Image
orIEnumerable<Image>
(if ‘Pick only images’ is checked), but now only contains items that actually inherit from this type (as opposed to throwing exceptions):If this class is added to a project, it will automatically be registered and replace the default PVC. This could be a lot cleaner if the default
MediaPickerValueConverter
is updated with my comments from https://github.com/umbraco/Umbraco-CMS/pull/6034#issuecomment-528327808, but it works and keeps duplicated code to a minimum.@nul800sebastiaan Make sure to update option 2, as it currently can throw a
NullReferenceException
if the preview image isn’t of typeImage
. @NikRimington’s https://github.com/umbraco/Umbraco-CMS/pull/6034#issuecomment-527425298 might also be updated. Better provide correct working examples, especially for the people who like to copy-paste 😉Using pattern-matching, this can be written as: