Copied to clipboard

Flag this post as spam?

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


  • MB 113 posts 422 karma points
    Nov 02, 2020 @ 06:24
    MB
    0

    Find PropertyValueConverter from PropertyEditor Alias ?

    I have a PropertyType and its PropertyEditor Alias, and I now want to find the PropertyValueConverter that returns its value - is that possible?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 02, 2020 @ 08:55
    Lee Kelleher
    1

    Hi MB,

    If you have the PublishedPropertyType, then you can search the PropertyValueConverters collection for it.

    Here's a quick snippet of how you could do it...

    // however you get the `propertyType`?
    
    var converter = Current.PropertyValueConverters.FirstOrDefault(x => x..IsConverter(propertyType) == true);
    if (converter != null)
    {
        // do your thang!
    }
    

    Note, using Umbraco.Core.Composing.Current really depends on where/how you are coding this. If it's in a controller, then you're better using Dependency Injection - injecting the PropertyValueConverterCollection instance instead.

    (If you'd like more info about Dependency Injection in Umbraco, there are details in the docs: https://our.umbraco.com/documentation/reference/using-ioc/)

    Hope this helps?

    Cheers,
    - Lee

  • MB 113 posts 422 karma points
    Nov 02, 2020 @ 11:20
    MB
    0

    Thanks - that's definitely advanced my cause a bit - using IsConverter(type) to find the matching Converter is on the track of what I'm trying to achieve - but I'm needing to do this for Umbraco-v7.

    I suspect I could do something similar using:

    PropertyValueConvertersResolver.Current.Converters.FirstOrDefault(x => x.IsConverter(publishedPropertyType))
    

    However, IsConverter() requires a PublishedPropertyType, and I currently only have a PropertyType and its Alias which I fetched using the ContentTypeService - not sure how to get from the PropertyType to the PublishedPropertyType

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 02, 2020 @ 12:17
    Lee Kelleher
    1

    Hi MB,

    It's been a while since I'd looked at v7's APIs, trying to recall how we did this for the original Nested Content package - found some bits and pieces.

    To get the PublishedPropertyType, you first need the PublishedContentType, which you can do as follows...

    var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias);
    

    For reference, I'd copied from Nested Content code: https://github.com/umbraco/Umbraco-CMS/blob/release-7.15.6/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentPublishedPropertyTypeExtensions.cs#L65

    Then from there, you can get the PublishedPropertyType...

    var publishedPropertyType = publishedContentType.GetPropertyType(propertyAlias);
    

    For reference, I'd copied from Nested Content code: https://github.com/umbraco/Umbraco-CMS/blob/release-7.15.6/src/Umbraco.Web/PropertyEditors/ValueConverters/NestedContentPublishedPropertyTypeExtensions.cs#L76

    Once you have the publishedPropertyType, then you can find out the value converter for it.

    Hope this helps?

    Cheers,
    - Lee

  • MB 113 posts 422 karma points
    Nov 02, 2020 @ 14:05
    MB
    0

    Cheers, that helped a lot, and I got to where I wanted to be, only to hit a problem that requires me to go and re-think this thing.

    Basically, I was building a custom backoffice tool to list the property editors on a known composition, and the valuetypes they return. I was working my way towards using GetPropertyValueType(PublishedPropertyType) of the PropertyValueConverter as it seemed to be documented as an interface method, but that was a no-go. So I then dug into the API docs, and it seems that's not actually the case (not sure why the discrepancy) so I can't use that approach to find the converted type ...this needs some more thought.

Please Sign in or register to post replies

Write your reply to:

Draft