Copied to clipboard

Flag this post as spam?

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


  • Bekker 4 posts 72 karma points
    Aug 26, 2021 @ 12:31
    Bekker
    0

    Calling built in value converter from custom value converter

    Hi

    I'm building some custom property editors, that makes use some of the built in editors fx. the "Multi Url Picker" or "Media Picker".

    Here i might end up with a value like:

    {
        someValue: 'lorem',
        image: {...} //media picker value
        link: {...} //Multi url picker value
    }
    

    Can i somehow use the built in value converters inside my own value converter, fx. https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs to convert parts of my model, or do i need to basicly have duplicate code in my custom value converter?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Aug 26, 2021 @ 15:38
    Søren Gregersen
    0

    Hi,

    Say you store

    {
       "text": "text 1",
       "image": "udi://...",
    }
    

    You would need a value converter that understands that format. You could use the JsonValueConverter if you setup your property editor to have a ValueType of Json.

    If you want the image to return an IPublishedContent instead, you would need a model for storing, and another for display.

    public class StoredValue
    {
        public string text{get;set;}
        public string image{get;set;}
    }
    
    public class DisplayValue
    {
        public string Text{get;set;}
        public IPublishedContent Image{get;set;}
    }
    

    Looking at https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/PropertyEditors/ValueConverters/ContentPickerValueConverter.cs as a base, you would need to override GetPropertyValueType and ConvertSourceToIntermediate

    public class YourValueConverter : PropertyValueConverterBase
    {
        private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
    
        public YourValueConverter(IPublishedSnapshotAccessor  publishedSnapshotAccessor)
        {
            _publishedSnapshotAccessor = publishedSnapshotAccessor;
        }
    
        public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
            => typeof (DisplayValue);
    
    
        public virtual object ConvertSourceToIntermediate(IPublishedElement owner, 
                IPublishedPropertyType propertyType, object source, bool preview)
            => JsonConvert.DeserializeObject<StoredValue>((string)source);
    
        public virtual object ConvertIntermediateToObject(IPublishedElement owner,
            IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) 
        {
          var value = inter as Value;
          var image = GetImage(value.image);
           return new DisplayValue { 
               Text = value.text,
               Image = image,
          }
       }
    
       private IPublishedContent GetImage(string image){
         if( GuidUdi.TryParse( image, out var udi )) {
             return _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(udi.Guid);
          }
          return null;
       }
    
    }
    

    The above code is just a mockup - not tested... :)

    HTH :)

  • Bekker 4 posts 72 karma points
    Aug 30, 2021 @ 18:17
    Bekker
    0

    Hi Søren

    Thanks for your reply, though not really what i'm looking for. Maybe i wasn't clear in my question.

    For example they new Media picker (v3) has a more complex value than just a UDI. It contains crops, focal points etc.

    So instead of me replicating the logic from the core Media Picker Value Converter. https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerWithCropsValueConverter.cs

    I'm trying to figure out if you can just call/use that instead inside my own value converter for parts of my model.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Aug 30, 2021 @ 19:44
    Søren Gregersen
    0

    I guess you could create an instance of the converter and call it?

Please Sign in or register to post replies

Write your reply to:

Draft