Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Ligtenberg 53 posts 214 karma points
    Jun 11, 2015 @ 07:04
    John Ligtenberg
    0

    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

  • Robert Foster 453 posts 1783 karma points MVP admin c-trib
    Jun 11, 2015 @ 07:34
    Robert Foster
    0

    Probably best to use IPublishedContent as the type; then you can just use the ImageCropper extensions.

  • Lee Kelleher 3993 posts 15620 karma points MVP 12x admin c-trib
    Jun 15, 2015 @ 05:40
    Lee Kelleher
    0

    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

  • Matt Brailsford 3630 posts 19227 karma points MVP 9x c-trib
    Jun 15, 2015 @ 08:29
    Matt Brailsford
    1

    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

  • Jeavon Leopold 3042 posts 13382 karma points MVP 9x admin c-trib
    Jun 16, 2015 @ 17:25
    Jeavon Leopold
    3

    Hello,

    Here's a value converter I prepared earlier:

    namespace MyProject.ValueConverters
    {
        using Newtonsoft.Json;
        using System;
        using Umbraco.Core;
        using Umbraco.Core.Models.PublishedContent;
        using Umbraco.Core.PropertyEditors;
        using Umbraco.Web.Models;
    
        public class ImageCropperPropertyConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
        {
            public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
            {
                try
                {
                    return JsonConvert.DeserializeObject<ImageCropDataSet>(source.ToString());
                }
                catch
                {
                    return null;
                }
            }
    
            public override bool IsConverter(PublishedPropertyType propertyType)
            {
                return Constants.PropertyEditors.ImageCropperAlias.InvariantEquals(propertyType.PropertyEditorAlias);
            }
    
            public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
            {
                return PropertyCacheLevel.Content;
            }
    
            public Type GetPropertyValueType(PublishedPropertyType propertyType)
            {
                return typeof(ImageCropDataSet);
            }
        }
    }
    

    We also have created a GetCropUrl extension method on ImageCropDataSet which I could share if useful...?

    Jeavon

  • John Ligtenberg 53 posts 214 karma points
    Aug 06, 2015 @ 14:11
    John Ligtenberg
    100

    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

  • Jeavon Leopold 3042 posts 13382 karma points MVP 9x admin c-trib
    Aug 06, 2015 @ 15:18
    Jeavon Leopold
    0

    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

  • Jeavon Leopold 3042 posts 13382 karma points MVP 9x admin c-trib
    Jun 16, 2015 @ 18:16
    Jeavon Leopold
    0

    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...?

  • Dan White 206 posts 510 karma points c-trib
    Oct 07, 2015 @ 00:11
    Dan White
    0

    @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).

    • HasImage and HasCrop are always false
    • GetCropUrl returns something like Umbraco.Web.Models.ImageCropDataSet?mode=pad&rnd=130886239890000000

    Any ideas?

  • Jeavon Leopold 3042 posts 13382 karma points MVP 9x admin c-trib
    Oct 14, 2015 @ 10:59
    Jeavon Leopold
    0

    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

  • Damian Green 452 posts 1433 karma points
    Oct 12, 2015 @ 22:36
    Damian Green
    0

    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.

  • Lee Kelleher 3993 posts 15620 karma points MVP 12x admin c-trib
    Oct 14, 2015 @ 10:40
    Lee Kelleher
    0

    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

  • Damian Green 452 posts 1433 karma points
    Oct 25, 2015 @ 21:31
    Damian Green
    0

    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.

        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"); }
        }
    
  • Lee Kelleher 3993 posts 15620 karma points MVP 12x admin c-trib
    Oct 26, 2015 @ 07:57
    Lee Kelleher
    1

    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.

    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")

    Some doco here:
    http://umbraco-ditto.readthedocs.org/en/latest/usage-advanced-valueresolvers/


    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.

    Some doco here:
    http://umbraco-ditto.readthedocs.org/en/latest/usage-advanced-typeconverters/


    I hope this helps?

    Cheers,
    - Lee

  • Damian Green 452 posts 1433 karma points
    Oct 26, 2015 @ 10:56
    Damian Green
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft