Copied to clipboard

Flag this post as spam?

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


  • Ram Prahalad 11 posts 101 karma points
    Jul 20, 2022 @ 20:58
    Ram Prahalad
    0

    Equivalent of PropertyValueConverterBase in Umbraco 8.5.1

    Hello All,

    I have this class in version 7.10 and I am trying to find its equivalent in 8.5 version. What is the equivalent of PropertyValueConverterBase class and those PropertyvalueType annotation classes?

    Any help would be greatly appreciated!

    Thanks

    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.PropertyEditors;
    
    namespace PropertyValueConverters
    {
        [PropertyValueType(typeof(bool))]
        [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
        public class TrueFalseValueConverter : PropertyValueConverterBase
        {
            public override bool IsConverter(PublishedPropertyType propertyType)
            {
                return propertyType.PropertyEditorAlias.Equals("USN.TrueFalse");
            }
    
            public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
            {
                // in xml a boolean is: string
                // in the database a boolean is: string "1" or "0" or empty
                // typically the converter does not need to handle anything else ("true"...)
                // however there are cases where the value passed to the converter could be a non-string object, e.g. int, bool
    
                if (source is string)
                {
                    var str = (string)source;
    
                    if (str == null || str.Length == 0 || str == "0")
                        return false;
    
                    if (str == "1")
                        return true;
    
                    bool result;
                    if (bool.TryParse(str, out result))
                        return result;
    
                    return false;
                }
    
                if (source is int)
                    return (int)source == 1;
    
                if (source is bool)
                    ret`enter code here`urn (bool)source;
    
                // default value is: false
                return false;
            }
    
            public override object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
            {
                // source should come from ConvertSource and be a boolean already
                return (bool)source ? "1" : "0";
            }
        }
    }
    
  • Angel 50 posts 106 karma points
    Apr 19, 2023 @ 14:53
    Angel
    0

    hi there,

    Just wondering if you managed to figure this out?

    TIA

Please Sign in or register to post replies

Write your reply to:

Draft