Copied to clipboard

Flag this post as spam?

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


  • Victoria 14 posts 114 karma points
    Feb 11, 2019 @ 22:25
    Victoria
    0

    Rendering custom properties in a template

    Hey there,

    I've created a custom property (addressFinder), with a valueType "JSON", and 3 properties in the JSON object (addressLatitude, addressLongitude, and locationAddress). Here's the package.manifest:

    enter image description here

    I'm using the data type in a document type called 'Office Location' where I'm wanting admin users to enter map locations that I'm going to use for a single Google map on another page.

    I'm trying to render out an array of the location data from addressFinder on another page (a document type called 'Map'), and was just wondering how I access the stored JSON data? I'm actually using a partial view macro file within a page, but I'm assuming it's the same syntax.

    I tried both of the following to no avail (just to see if I could get a value printed out):

    enter image description here enter image description here

    Is anyone able to point me in the right direction in terms of accessing the fields within my JSON object?

    Many thanks,

    Victoria

  • Chris Evans 137 posts 353 karma points c-trib
    Feb 12, 2019 @ 01:10
    Chris Evans
    1

    You'll probably want to implement a custom property convertor.

    This would handle deserializing the JSON into an object that you can use in your partial view (and anywhere else on the site that might use the same data type).

    There is a good example of how to do that in the following forum post : https://our.umbraco.com/forum/umbraco-7/developing-umbraco-7-packages/61760-A-question-about-property-converters#comment-210057

    Hope that helps!

  • mcgrph 35 posts 162 karma points
    Jul 23, 2019 @ 20:56
    mcgrph
    1

    Chris pointed me in the right direction, as I was also searching for the same issue you have Victoria. With some research and digging trough the Umbraco core I've come up with the following (which is working for me):

    You create a custom converter class, where you need to make sure to give the correct name of the property type alias (which you define in the package.manifest - in my case "myPropertyTypeAlias")

    public class MyCustomerPropertyValueConverter : IPropertyValueConverter
    {
        public bool IsConverter(IPublishedPropertyType propertyType)
        {
            return propertyType.Alias == "myPropertyTypeAlias";
        }
    
        public bool? IsValue(object value, PropertyValueLevel level)
        {
            switch (level)
            {
                case PropertyValueLevel.Source:
                    return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string)value) == false);
                default:
                    throw new NotSupportedException($"Invalid level: {level}.");
            }
        }
    
        public Type GetPropertyValueType(IPublishedPropertyType propertyType)
        {
            return propertyType.GetType();
        }
    
        public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
        {
            return PropertyCacheLevel.Snapshot;
        }
    
        public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
        {
            return MyCustomModel.Deserialize(source as string);
        }
    
        public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            return inter;
        }
    
        public object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            return inter?.ToString() ?? string.Empty; ;
        }
    }
    

    Then you have your model MyCustomModel:

    public class MyCustomModel
    {
        [JsonProperty("address")]
        public string Address { get; set; }
    
        [JsonProperty("latitude")]
        public double Latitude { get; set; }
    
        [JsonProperty("langitude")]
        public double Langitude { get; set; }
    
        public static MyCustomModel Deserialize(string json)
        {
            if (json == null || !json.StartsWith("{") || !json.EndsWith("}")) return null;
            return JsonConvert.DeserializeObject<MyCustomModel>(json);
        }
    }
    

    And in your (partial)view you simply call it like this:

    @{ var addressFinder = Model.Value<MyCustomModel>("myPropertyTypeAlias"); }
    
    <p>Address: @addressFinder.Address</p>
    <p>Latitude: @addressFinder.Latitude</p>
    <p>Langitude: @addressFinder.Langitude</p>
    

    I hope this helps. Keep in mind, that I did this with Umbraco 8.1.0.

Please Sign in or register to post replies

Write your reply to:

Draft