IPublishedContent media items as MediaWithCrops list?
I am using the Media picker (v3) datatype in umbraco 9 for a property to either pick an image or folder.
When an image is picked then the model for the single item is MediaWithCrops which is great. When is a folder is picked I want to obtain all of the images within it (via IPublishedContent.Children) but the model for these items is my Image doctype, not MediaWithCrops. Is there an easy way to type or convert these to a list of MediaWithCrops?
Hopefully there is an easy way and this is just a case of my brain going on Christmas holidays early 😃
hi Søren, thanks for the reply. sorry just to clarify, yes i know how to get the children as a list of IPublishedContent. What i was actually trying to achieve is the list of children as type MediaWithCrops (like modelsbuilder would do automatically). Ive been looking at the umbraco source and it doesn't look like the PropertyValueConverter can be repurposed/used this way, so i guess i just wondered if someone had a magic way i wasn't thinking of :D
actually, as i thought about it more, i didn't really need to use MediaWithCrops as there is never going to be "local crops" in this particular scenario of mine, and there is a GetCropUrl() extension for IPublishedContent media that will give me the "global crops" that I'm after. So this is the solution for me, but will leave this thread here anyway in case someone else has Christmas vacation brain and finds themselves here looking for ideas too 😁
If you create a new media picker and set it to allow both Image and Folder, and enable multiple items. Then define a crop, lets call it "thumbnail"
Then add the new datatype to a doctype.
This allows the user to select either an image or a folder, the idea being that when rendering, if its a folder, then select all the images in that folder.
However when selecting the child images, the list returned is of IPublishedContent and therefore you cannot select the named local crop.
ie, this won't work
if (item.ContentType.Alias == "Folder")
{
var images = item.ChildrenOfType("Image");
foreach (var img in images)
{
<img src="@img.GetCropUrl("thumbnail")" />
}
}
In my head this kinda makes sense since there nothing to say local crops should be created for all children of a selected folder (at least I don't think there is)
So I can only think of two solutions:
1. Create global crops instead
2. don't use crop alias and use instead GetCropUrl(w, h)
Another issue is that an editor should be able to change the crop when editing the page, which they can do when they select an image, but not when they select a folder (ie they can't set the crops for the images within the folder from the content page)
IPublishedContent media items as MediaWithCrops list?
I am using the Media picker (v3) datatype in umbraco 9 for a property to either pick an image or folder. When an image is picked then the model for the single item is MediaWithCrops which is great. When is a folder is picked I want to obtain all of the images within it (via IPublishedContent.Children) but the model for these items is my Image doctype, not MediaWithCrops. Is there an easy way to type or convert these to a list of MediaWithCrops?
Hopefully there is an easy way and this is just a case of my brain going on Christmas holidays early 😃
Thanks
Hi Andrew
You can get the IPublishedContent in the Content property, so the children would be Model.YourMediaPickerAlias.Content.Children()
hi Søren, thanks for the reply. sorry just to clarify, yes i know how to get the children as a list of IPublishedContent. What i was actually trying to achieve is the list of children as type MediaWithCrops (like modelsbuilder would do automatically). Ive been looking at the umbraco source and it doesn't look like the PropertyValueConverter can be repurposed/used this way, so i guess i just wondered if someone had a magic way i wasn't thinking of :D
thanks
actually, as i thought about it more, i didn't really need to use MediaWithCrops as there is never going to be "local crops" in this particular scenario of mine, and there is a GetCropUrl() extension for IPublishedContent media that will give me the "global crops" that I'm after. So this is the solution for me, but will leave this thread here anyway in case someone else has Christmas vacation brain and finds themselves here looking for ideas too 😁
Hi Andrew
There is a Content property in MediaWithCrops that lets you access the IPublishedContent of your selected media.
I've just come across this issue.
If you create a new media picker and set it to allow both Image and Folder, and enable multiple items. Then define a crop, lets call it "thumbnail"
Then add the new datatype to a doctype.
This allows the user to select either an image or a folder, the idea being that when rendering, if its a folder, then select all the images in that folder. However when selecting the child images, the list returned is of IPublishedContent and therefore you cannot select the named local crop.
ie, this won't work
In my head this kinda makes sense since there nothing to say local crops should be created for all children of a selected folder (at least I don't think there is)
So I can only think of two solutions: 1. Create global crops instead 2. don't use crop alias and use instead GetCropUrl(w, h)
Another issue is that an editor should be able to change the crop when editing the page, which they can do when they select an image, but not when they select a folder (ie they can't set the crops for the images within the folder from the content page)
Unless someone else has another idea?
is working on a reply...