Copied to clipboard

Flag this post as spam?

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


  • Lachlann 344 posts 626 karma points
    Jun 11, 2016 @ 16:37
    Lachlann
    0

    Ditto 0.9 writing Custom type convertors

    Hey guys, Ditto is awesome, I have just started using it beyond the standard mapping of simple Umbraco property types.

    I recently created my own custom Umbraco property type which allows the user to pick a 'Time' which is basically two values int Hours and int Mins.

    The value passed back from Umbraco is in JSON format: { "hours": 9, "mins": 0 }

    I have read through the docs and a lot of posts around a similar issues but i cant seem to hit on the correct solution for creating a Custom type converter. I am using Ditto 0.9 and I have read that the type converter process has been overhauled but I just cant seem to get anything working. I always seem to get an Error relating to Ditto trying to case a 'String' to my custom type.

    Any help would be very much appreciated.

  • Lee Kelleher 4007 posts 15711 karma points MVP 12x admin c-trib
    Jun 20, 2016 @ 13:22
    Lee Kelleher
    0

    Hey Lachlann,

    I've been away at CG16, only just got chance to reply.

    Did you get any further with your custom Processor (or PropertyValueConverter)?

    If it's cool, please do share your code (or what you've got so far), we'd be happy to help/advise.

    Cheers,
    - Lee

  • Lachlann 344 posts 626 karma points
    Jun 21, 2016 @ 10:13
    Lachlann
    0

    Hi Lee, No problem, I hope CG16 was great! I did not manage to get my custom processor working yet. I am not even sure if it is a custom processor I want. Basically I have my custom property editor (which returns the json described above.) I want to be able to map that to a Model:

    public class Time
    {
        public string Hours { get; set;}
        public string Mins { get; set; }
    }
    

    I have tried using all the different techniques I found whilst googling around but nothing seems to work. I normal end up with an Error saying something above not being able to convert a string to type "Time".

    I wondered if you had an example of what is required to use Ditto with a custom property type ?

  • Lee Kelleher 4007 posts 15711 karma points MVP 12x admin c-trib
    Jun 21, 2016 @ 11:40
    Lee Kelleher
    100

    Hey Lachlann,

    OK, you definitely want to make a custom Property Value Converter for your property-editor. Just to be clear, this is an Umbraco thing, not a Ditto thing :-)

    For reference there are docs on how to make these:
    https://our.umbraco.org/Documentation/Extending/Property-Editors/value-converters

    An example would be like this...

    public class Time
    {
        public int Hours { get; set; }
        public int Mins { get; set; }
    }
    
    [Umbraco.Core.PropertyEditors.PropertyValueType(typeof(Time))]
    public class MyTimePropertyValueConverter : Umbraco.Core.PropertyEditors.PropertyValueConverterBase
    {
        public override bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias == "YourPropertyEditorAlias";
        }
    
        public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            if (source == null)
                return null;
    
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Time>(source.ToString());
        }
    }
    

    What this does is that whenever you make a call to Model.Content.GetPropertyValue("someAlias"), then Umbraco will use your custom PropertyValueConverter to convert the value to your desired object-type, (in this case your Time model).

    ...and when you call Model.Content.GetPropertyValue<Time>("someAlias"), then Visual Studio will understand the object-type and use intellisense, etc.

    So far this has nothing to do with Ditto. Where Ditto comes into play is that behind the scenes it will make a call to GetPropertyValue, meaning that your custom PropertyValueConverter will be used.

    I hope all this makes sense?

    Cheers,
    - Lee

  • Lee Kelleher 4007 posts 15711 karma points MVP 12x admin c-trib
    Jun 21, 2016 @ 11:43
    Lee Kelleher
    1

    On a side-note, if you want to see examples of how people have been implementing their own custom PropertyValueConverters, there are loads on GitHub.

    https://github.com/search?l=csharp&q=Umbraco+PropertyValueConverterBase&ref=searchresults&type=Code&utf8=%E2%9C%93

  • Lachlann 344 posts 626 karma points
    Jun 21, 2016 @ 12:18
    Lachlann
    0

    Amazing, thanks Lee. Your code worked first time. I learnt something new about Umbraco and my code is working, what a Tuesday :) L

  • Lee Kelleher 4007 posts 15711 karma points MVP 12x admin c-trib
    Jun 21, 2016 @ 13:59
    Lee Kelleher
    0

    Cool, happy to hear that it worked for you! :-)

Please Sign in or register to post replies

Write your reply to:

Draft