Copied to clipboard

Flag this post as spam?

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


  • Tom Harrington 10 posts 30 karma points
    Jul 16, 2014 @ 17:22
    Tom Harrington
    0

    Recursive related links and property value convertor

    I have a custom property value converter which converts RelatedLinks properties into a custom object representing the links (UmbracoRelatedLink).

    public class RelatedLinkConverter : PropertyValueConverterBase
    {
        public override bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias.Equals(Constants.PropertyEditors.RelatedLinksAlias);
        }
    
        public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            return JsonConvert.DeserializeObject<List<UmbracoRelatedLink>>(source.ToString());
        }
    }
    

    This is working fine apart from sometimes when I use it recursively. For some reason, and I haven't yet worked out why, when a page is published on which the related links property has no links set then the value coming into the property value converter is [] (i.e. an empty json object).

    The problem is that umbraco then decides that the value is set on that node, and doesn't bother to look up the ancestry tree to find a node where the list does have some values.

    Can somebody explain how I can either make the publish save the empty list as a genuinely empty property, or somehow have my property value converter handle this scenario correctly? You cannot just check for "[]" and return null, as an ArgumentNullException is thrown when you use it.

    I have a pretty rubbish work around where I manually check whether the returned list is empty and do the recursion by hand, but this isn't pretty and I rather have something clean which works properly.

Please Sign in or register to post replies

Write your reply to:

Draft