Copied to clipboard

Flag this post as spam?

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


  • Jesse Andrews 191 posts 716 karma points c-trib
    Nov 08, 2021 @ 21:46
    Jesse Andrews
    0

    Nested Content is missing GetImportValue implementation

    I built out a custom control that needs a mapper to migrate its data from one environment to another. Unfortunately, there is a gap in the functionality of NestedContentMapper, as it has a GetExportValue implementation, but no GetImportValue implementation. Since my custom control is inside a nested content element, it doesn't get properly imported due to this.

    This also appears to be an issue in the v9 branch of usync.

  • Jesse Andrews 191 posts 716 karma points c-trib
    Feb 12, 2022 @ 00:19
    Jesse Andrews
    100

    I ended up creating a fixed version of NestedContentMapper to handle this issue. The code is as follows for anyone that runs into this problem.

    public class NestedContentMapperFixed : NestedContentMapper {
        private readonly string docTypeAliasValue = "ncContentTypeAlias";
        private readonly Lazy<SyncValueMapperCollection> mapperCollection;
    
        public NestedContentMapperFixed(IEntityService entityService, IContentTypeService contentTypeService, IDataTypeService dataTypeService, Lazy<SyncValueMapperCollection> mapperCollection) : base(entityService, contentTypeService, dataTypeService) {
            this.mapperCollection = mapperCollection;
        }
    
        public override string Name => "Nested Content Mapper Fixed";
    
        public override string GetImportValue(string value, string editorAlias) {
            if (string.IsNullOrWhiteSpace(value) || !value.DetectIsJson()) return value.ToString();
    
            var nestedJson = JsonConvert.DeserializeObject<JArray>(value);
            if (nestedJson == null || !nestedJson.Any()) return value.ToString();
    
            foreach (var item in nestedJson.Cast<JObject>()) {
                var docType = GetDocType(item, this.docTypeAliasValue);
                if (docType == null) continue;
    
                GetImportProperties(item, docType);
            }
    
            return JsonConvert.SerializeObject(nestedJson);
        }
    
        protected JObject GetImportProperties(JObject item, IContentType docType) {
            foreach (var property in docType.CompositionPropertyTypes) {
                if (item.ContainsKey(property.Alias)) {
                    var value = item[property.Alias];
                    if (value != null) {
                        var mappedVal = mapperCollection.Value.GetImportValue(value.ToString(), property.PropertyEditorAlias);
                        item[property.Alias] = mappedVal != null ? JToken.FromObject(mappedVal) : null; // .GetJsonTokenValue();
                    }
                }
            }
    
            return item;
        }
    }
    

    I just extended the NestedContentMapper to add in the missing GetImportValue method. Then I needed to remove the existing NestedContentMapper with

    composition.WithCollectionBuilder<SyncValueMapperCollectionBuilder>().Remove<NestedContentMapper>();
    

    in a composer (my code is for umbraco 8, so the syntax will be a little different when working in umbraco 9).

Please Sign in or register to post replies

Write your reply to:

Draft