Last active
November 17, 2020 10:53
-
-
Save KevinJump/cd22910c2f02e2d02160a2c97e3f5da2 to your computer and use it in GitHub Desktop.
Example code to use the Translation Manager's internal link updater to update links between two sites.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Umbraco.Core.Models; | |
using Jumoo.TranslationManager.Core.Services; | |
using Jumoo.TranslationManager.LinkUpdater; | |
namespace MyLinkUpdater | |
{ | |
/// <summary> | |
/// Update links inside content based on the setup in translation manager. | |
/// </summary> | |
/// <remarks> | |
/// This should work as long as: | |
/// 1. The sites are in a translation set | |
/// 2. The content nodes are related (this happens when you copy with relate to orginal) | |
/// | |
/// they don't need to have been though the actual translation process, just be linked | |
/// (and well have sort of the same content!) | |
/// | |
/// you can check links using the LinkedPages setup. | |
/// | |
/// the linkupdater goes through the properties of a node, and finds all links in : | |
/// text, pickers, grid, nested content, etc.. | |
/// | |
/// When if finds a link that is pointing to something in the 'master' site it uses | |
/// the relation service to find the equivilant in the 'target' site and it updates | |
/// the link on your target node to point to that. | |
/// | |
/// if you enumerated through your target site and called this for each node it would update | |
/// all the links | |
/// </remarks> | |
public class NodeLinkUpdater | |
{ | |
private readonly LinkUpdater _linkUpdater; | |
private readonly TranslationSetService _setService; | |
private readonly TranslationNodeService _nodeService; | |
public NodeLinkUpdater( | |
TranslationSetService setService, | |
TranslationNodeService nodeService, | |
LinkUpdater linkUpdater) | |
{ | |
_nodeService = nodeService; | |
_setService = setService; | |
_linkUpdater = linkUpdater; | |
} | |
public void UpdateLinks(IContent node) | |
{ | |
// get you the translation set based on the content node | |
// assume its only in one set? | |
var set = _setService.GetSetsByTargetPath(node.Path) | |
.FirstOrDefault(); | |
if (set == null) return; | |
// the 'site' in the set that this node belongs to. | |
var targetSite = _setService.GetTargetSite(set, node.Path); | |
if (targetSite == null) return; | |
// get the content node on the master site, that is the | |
// equivilant of the one we are looking at on the target | |
var sourceNode = _nodeService.GetMasterFromTarget(node, set); | |
if (sourceNode == null) return; | |
// update the links for this page. | |
_linkUpdater.ProcessLinks(set, targetSite, sourceNode, node, true, -1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment