I'm trying to use the Umbraco 7 ImageCropper with Ditto, but haven't succeeded yet in getting any value for the ImageCropper field after mapping. The other fields are mapping fine. I'm not quite sure what the type of the field should be in the model class. I've tried ImageCropDataSet, but that doesn't seem to work.
Or is it necessary to write a TypeConverter for this type of field?
I think this is likely due to the fact that within Umbraco there is no value converter that resolves the image cropper value to a strongly typed model. There does seem to be the concept of an ImageCropDataSet, which looks like this is what they convert to internally, however this hasn't been encapsulated in a value converter for when you wish to retrieve the raw value, so my guess is that it will come out as a string, and you'd need to JSON convert it (this is all HQ do internally).
When resolving a value to a type, ditto will use the inbuilt value converters first, and then the attributed type converters on your model if one is supplied.
I think the right thing here would be to create a value converter for the image cropper that can resolve an ImageCropDataSet. If you then needed this data in an alternative format, you could then use a custom Type Converter on your view model to convert it into something new.
Thanks very much for ImageCropperPropertyConverter, I've implemented it in my project and it works fine. If you could share the GetCropUrl extension that would be great!
I have been thinking for a while about adding this value converter and the GetCropUrl helper to my Core value converters package as we use it all the time as we use Ditto.
We think of value converters as "fixing" the core, so that everything is IPublishedContent or other existing core/third party models and then use TypeConverters for Ditto to convert to specific ViewModels etc...
It would be a breaking change so I would create a v3 release and add a legacy setting for upgrades....
Your ImageCropperPropertyConverter found here seems to break the default methods for working with the image cropper (https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper).
HasImage and HasCrop are always false
GetCropUrl returns something like Umbraco.Web.Models.ImageCropDataSet?mode=pad&rnd=130886239890000000
Yes it will do that (and also stop .Url working on Media) as Umbraco doesn't expect ImageCropDataSet, it expects a string.
This is still very much WIP but I have added the Cropper converter to v3 of my Core Property Converters package and have also added extensions methods so that you can still use GetCropUrl etc...
You can see the files containing the extension methods here ImageCropperBaseExtensions.cs & ImageCropperTemplateExtensions.cs
I have had a bash at this and not quite getting it working either. I have added the converter and that is getting triggered but when i try and access the crops im getting a null value for the crops property.
Not sure whats going wrong...
My model property:
public ImageCropDataSet Image { get; set; }
and also have the Converter above.
If i remove the converter and use IPublishedContent i can get the crop fine but having no luck with ditto and converters for it.
Once the PVC (that Jeavon supplied above) has ran, the value passed to Ditto would be of ImageCropDataSet type, so Ditto "should" map it correctly - along with populated Crop values.
Let me know if you think I'm missing something, I'm happy to update the unit-test, etc.
Can see your test works ok but if I use ImageCropDataSet I get a null object. Set its type to IPublishedContent and i can see the properties in the JSON and get the crop using razor.
Why could this be? Just doesn't work if i use ImagecropDataSet. I've also tied setting the property name in UmbracoProperty attribute as i a using a prefix. Wondered if that was causing issues.
public IPublishedContent Image { get; set; }
or
public ImageCropDataSet Image { get; set; }
When i add this code the Image is null so get no crop:
[DittoIgnore]
public string ImageCrop
{
get { return Image.GetCropUrl("infoBlock"); }
}
Is your "image" property a Media Picker or an Image Cropper editor?
If it is a Media Picker, then Ditto is trying to get the media node and mapping it to a ImageCropDataSet - which would return null.
(As Ditto has no understanding of the media node's property alias for the Image Cropper).
The options are to either...
1. Add a nested POCO object to represent your media nodes, e.g.
public class MediaContainer
{
[UmbracoProperty("umbracoFile")] // or "infoBlock" whatever the actual alias is?
public ImageCropDataSet Image { get; set; }
}
then have the content node's POCO to use:
public MediaContainer Image { get; set; }
It might feel a bit cumbersome, but the content/media nodes are nested, so would follow the same representation.
2. Write a custom ValueResolver that will get the int value from the "image" property, get the media node, then call .GetPropertyValue<ImageCropDataSet>("umbracoFile")
3. Write a custom TypeConverter that will convert an IPublishedContent into an ImageCropDataSet. To be honest, this is very similar to the ValueResolver approach, but happens more towards the end of the conversion process.
Ah yeah - thats it. I have put an image cropper on the Image file type and then a media picker on my doc type.
I've quickly changed my code to have a nested class as above and we have a result so i'll have a look at the other options tonight and see what works best for what I am doing.
Thanks for the help Lee! :) Knew i must have been doing something wrong.
I've been away from Umbraco for a few months but it feels like over a year with all the new features and packages... playing catch up.
ImageCropper mapping with Ditto
Hello,
I'm trying to use the Umbraco 7 ImageCropper with Ditto, but haven't succeeded yet in getting any value for the ImageCropper field after mapping. The other fields are mapping fine. I'm not quite sure what the type of the field should be in the model class. I've tried ImageCropDataSet, but that doesn't seem to work.
Or is it necessary to write a TypeConverter for this type of field?
John
Probably best to use IPublishedContent as the type; then you can just use the ImageCropper extensions.
Hi John,
It's a good question. I've never tried the ImageCropper with Ditto.
We'll do some tests with it - unless you've already figured it out? ;-)
Cheers,
- Lee
Hi John,
I think this is likely due to the fact that within Umbraco there is no value converter that resolves the image cropper value to a strongly typed model. There does seem to be the concept of an ImageCropDataSet, which looks like this is what they convert to internally, however this hasn't been encapsulated in a value converter for when you wish to retrieve the raw value, so my guess is that it will come out as a string, and you'd need to JSON convert it (this is all HQ do internally).
When resolving a value to a type, ditto will use the inbuilt value converters first, and then the attributed type converters on your model if one is supplied.
I think the right thing here would be to create a value converter for the image cropper that can resolve an ImageCropDataSet. If you then needed this data in an alternative format, you could then use a custom Type Converter on your view model to convert it into something new.
Matt
Hello,
Here's a value converter I prepared earlier:
We also have created a GetCropUrl extension method on ImageCropDataSet which I could share if useful...?
Jeavon
Hello Jeavon,
Thanks very much for ImageCropperPropertyConverter, I've implemented it in my project and it works fine. If you could share the GetCropUrl extension that would be great!
John
Hi John,
You can find it here ImageCropperBaseExtensions.cs and ImageCropperTemplateExtension.cs
I haven't finished my thinking how I'm fully going to approach this for v3 yet but it should get you going for now.
Jeavon
I have been thinking for a while about adding this value converter and the GetCropUrl helper to my Core value converters package as we use it all the time as we use Ditto.
We think of value converters as "fixing" the core, so that everything is IPublishedContent or other existing core/third party models and then use TypeConverters for Ditto to convert to specific ViewModels etc...
It would be a breaking change so I would create a v3 release and add a legacy setting for upgrades....
Any thoughts...?
@Jeavon:
Your ImageCropperPropertyConverter found here seems to break the default methods for working with the image cropper (https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper).
Any ideas?
Yes it will do that (and also stop .Url working on Media) as Umbraco doesn't expect ImageCropDataSet, it expects a string.
This is still very much WIP but I have added the Cropper converter to v3 of my Core Property Converters package and have also added extensions methods so that you can still use GetCropUrl etc...
You can see the files containing the extension methods here ImageCropperBaseExtensions.cs & ImageCropperTemplateExtensions.cs
The converter itself is here
I have had a bash at this and not quite getting it working either. I have added the converter and that is getting triggered but when i try and access the crops im getting a null value for the crops property.
Not sure whats going wrong...
My model property:
public ImageCropDataSet Image { get; set; }
and also have the Converter above.
If i remove the converter and use IPublishedContent i can get the crop fine but having no luck with ditto and converters for it.
Hi Damian,
I was curious about this, so I wrote a quick unit-test for Ditto for it...
https://github.com/leekelleher/umbraco-ditto/pull/126/files#diff-c5d9b5f2678093d4786c7cba6531496fR20
Once the PVC (that Jeavon supplied above) has ran, the value passed to Ditto would be of
ImageCropDataSet
type, so Ditto "should" map it correctly - along with populated Crop values.Let me know if you think I'm missing something, I'm happy to update the unit-test, etc.
Cheers,
- Lee
Hi,
Sorry - i've only just got to look at this.
Can see your test works ok but if I use ImageCropDataSet I get a null object. Set its type to IPublishedContent and i can see the properties in the JSON and get the crop using razor.
Why could this be? Just doesn't work if i use ImagecropDataSet. I've also tied setting the property name in UmbracoProperty attribute as i a using a prefix. Wondered if that was causing issues.
or
When i add this code the Image is null so get no crop:
Hi Damian,
Is your "image" property a Media Picker or an Image Cropper editor?
If it is a Media Picker, then Ditto is trying to get the media node and mapping it to a
ImageCropDataSet
- which would return null.(As Ditto has no understanding of the media node's property alias for the Image Cropper).
The options are to either...
1. Add a nested POCO object to represent your media nodes, e.g.
then have the content node's POCO to use:
It might feel a bit cumbersome, but the content/media nodes are nested, so would follow the same representation.
2. Write a custom
ValueResolver
that will get theint
value from the "image" property, get the media node, then call.GetPropertyValue<ImageCropDataSet>("umbracoFile")
Some doco here:
http://umbraco-ditto.readthedocs.org/en/latest/usage-advanced-valueresolvers/
3. Write a custom
TypeConverter
that will convert anIPublishedContent
into anImageCropDataSet
. To be honest, this is very similar to theValueResolver
approach, but happens more towards the end of the conversion process.Some doco here:
http://umbraco-ditto.readthedocs.org/en/latest/usage-advanced-typeconverters/
I hope this helps?
Cheers,
- Lee
Ah yeah - thats it. I have put an image cropper on the Image file type and then a media picker on my doc type.
I've quickly changed my code to have a nested class as above and we have a result so i'll have a look at the other options tonight and see what works best for what I am doing.
Thanks for the help Lee! :) Knew i must have been doing something wrong.
I've been away from Umbraco for a few months but it feels like over a year with all the new features and packages... playing catch up.
is working on a reply...