Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created April 25, 2019 08:50
Show Gist options
  • Save KevinJump/34e28162b169e1b62e76e7251f307d82 to your computer and use it in GitHub Desktop.
Save KevinJump/34e28162b169e1b62e76e7251f307d82 to your computer and use it in GitHub Desktop.
Translation Manager - Link Mapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jumoo.TranslationManager.Core.Models;
using Umbraco.Core;
using Umbraco.Core.Services;
namespace Jumoo.TranslationManager.LinkUpdater.LinkMappers
{
public class IDLinkMapper : LinkMapperBase, ILinkMapper
{
public IDLinkMapper(IContentService contentService, IRelationService relationService) :
base(contentService, relationService)
{ }
public string Name => "ID Link Mapper";
public string[] Editors => new string[] {
"Umbraco.ContentPicker",
"Umbraco.MultiNodeTreePicker"
};
public bool ReplaceFromSource => true;
public object UpdateLinkValues(TranslationSet set, int targetSiteId, object sourceValue, object targetValue)
{
if (set == null || sourceValue == null) return sourceValue;
var source = sourceValue.TryConvertTo<string>();
if (!source.Success) return sourceValue;
var ids = source.Result.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
var newIds = new List<int>();
var linkResolver = this.GetLinkResolver();
foreach(var id in ids)
{
if (int.TryParse(id, out int intId))
{
newIds.Add(linkResolver.ResolveLink(set, targetSiteId, intId));
}
}
return string.Join(",", newIds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment