Example #1
0
        public override object ConvertSourceToIntermediate(IPublishedElement owner, PublishedPropertyType propertyType, object source, bool preview)
        {
            if (source == null)
            {
                return(null);
            }
            var sourceString = source.ToString();

            if (sourceString.DetectIsJson())
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject <JArray>(sourceString);
                    //update the internal links if we have a context
                    if (UmbracoContext.Current != null)
                    {
                        var helper = new UmbracoHelper(_umbracoContextAccessor.UmbracoContext, _services);
                        foreach (var a in obj)
                        {
                            var type = a.Value <string>("type");
                            if (type.IsNullOrWhiteSpace() == false)
                            {
                                if (type == "internal")
                                {
                                    switch (propertyType.EditorAlias)
                                    {
                                    case Constants.PropertyEditors.Aliases.RelatedLinks:
                                        var strLinkId  = a.Value <string>("link");
                                        var udiAttempt = strLinkId.TryConvertTo <Udi>();
                                        if (udiAttempt)
                                        {
                                            var content = helper.PublishedContent(udiAttempt.Result);
                                            if (content == null)
                                            {
                                                break;
                                            }
                                            a["link"] = helper.Url(content.Id);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    return(obj);
                }
                catch (Exception ex)
                {
                    _logger.Error <RelatedLinksLegacyValueConverter>(ex, "Could not parse the string '{Json}' to a json object", sourceString);
                }
            }

            //it's not json, just return the string
            return(sourceString);
        }