Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Jumoo.TranslatePlus.Core;
using Jumoo.TranslatePlus.Core.Models;
using Jumoo.TranslatePlus.Core.ValueMappers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
namespace Jumoo.TranslatePlus.Core.ValueMappers
{
public class MultiUrlPickerValueMapper : BaseValueMapper, IValueMapper
{
public string Name => "Multi Url Picker";
public override string[] Editors => new [] { "RJP.MultiUrlPicker" };
public TranslationValue GetSourceValue(string displayName,
string propertyTypeAlias,
object value,
CultureInfoView culture)
{
if (value == null) return null;
var stringValue = (string)value;
if (stringValue.IsNullOrWhiteSpace()) return null;
var array = JsonConvert.DeserializeObject<JArray>(stringValue);
if (array == null || array.Count == 0) return null;
var translationValue = new TranslationValue(displayName, propertyTypeAlias);
for (var i = 0; i < array.Count; i++)
{
var lnk = array[i];
var lnkNameValue = new TranslationValue($"{displayName} {(array.Count > 1 ? "["+ i +"]": "")} Name", propertyTypeAlias)
{
Value = lnk.Value<string>("name")
};
translationValue.InnerValues.Add(i.ToString(), lnkNameValue);
}
return translationValue;
}
public object GetTargetValue(
string propertyTypeAlias,
object sourceValue,
TranslationValue values,
CultureInfoView sourceCulture,
CultureInfoView targetCulture)
{
if (sourceValue == null) return null;
var stringValue = (string)sourceValue;
if (stringValue.IsNullOrWhiteSpace()) return null;
var array = JsonConvert.DeserializeObject<JArray>(stringValue);
if (array == null || array.Count == 0) return null;
for (var i = 0; i < array.Count; i++)
{
var lnk = (JObject)array[i];
var nameValue = values.GetInnerValue(i.ToString());
if (nameValue != null)
{
lnk["name"] = nameValue.Value;
}
}
return JsonConvert.SerializeObject(array, Formatting.Indented);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment