Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2690 posts 8066 karma points MVP 3x admin c-trib
    Feb 05, 2019 @ 10:08
    Chriztian Steinmeier
    1

    Using Switcher with ModelsBuilder

    Hi,

    I use Switcher all the time, but I find it weird that ModelsBuilder doesn't know that it's "just" an int (it registers as object - not even a bool).

    I usually do this to get the value:

    bool isSuperAwesome = Model.GetPropertyValue<int>("Awesome") == 1;
    

    but I would love to know if it's possible to throw a custom PropertyEditorValueConvert (or whatever it's called) after it - and how I might do that without compiling DLLs etc. :)

    /Chriztian

  • Matthew Wise 189 posts 773 karma points c-trib
    Feb 05, 2019 @ 10:18
    Matthew Wise
    101

    Hi Chriztain,

    I have the code below, You can just added it your own solution. You can add it into App_Code folder if want but its not recommended :)

    Matt

    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.PropertyEditors;
    
    namespace <YourNamespace>
    {
        [PropertyValueType(typeof(bool))]
        [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
        public class SwitcherPropertyConverter : PropertyValueConverterBase
        {
    
            public override bool IsConverter(PublishedPropertyType propertyType)
            {
                return propertyType.PropertyEditorAlias.Equals("Our.Umbraco.Switcher");
            }
    
            public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
            {
                if (source == null) return false;
                bool value = false;
                if (source is int)
                {
                    value = (int)source == 1;
                }
                return value;
            }
        }
    }
    
  • Chriztian Steinmeier 2690 posts 8066 karma points MVP 3x admin c-trib
    Feb 05, 2019 @ 10:31
    Chriztian Steinmeier
    0

    Thanks Matt - that one goes straight into my folder of good stuff :-)

    /Chriztian

  • Bjarne Fyrstenborg 1136 posts 3218 karma points MVP 3x c-trib
    Feb 05, 2019 @ 10:36
    Bjarne Fyrstenborg
    0

    Hi Chriztian

    Yes, ModelsBuilder return an object by default unless there exists a property value converter to handle the return type, where the strongly typed syntax (and ModelsBuilder) know the return type.

    I could add a property value converter for this in the package as requested here https://github.com/bjarnef/Switcher/issues/6 but I find it a bit overkill the add another assembly just to handle this and when Umbraco core already have property value converters for this.

    I have created a issue here that it should be possible to re-use property value converters for simple types https://github.com/umbraco/Umbraco-CMS/issues/4419 (the original issue was created here https://issues.umbraco.org/issue/U4-10005)

    /Bjarne

  • Chriztian Steinmeier 2690 posts 8066 karma points MVP 3x admin c-trib
    Feb 05, 2019 @ 10:51
    Chriztian Steinmeier
    0

    Awesome - thanks for the explanation Bjarne!

    (I did think we'd discussed this earlier at a meetup :)

    Totally agree on the overkill part, too.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft