Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Last active April 30, 2019 18:16
Show Gist options
  • Save KevinJump/9551e9adbaebba1910771f7b40936649 to your computer and use it in GitHub Desktop.
Save KevinJump/9551e9adbaebba1910771f7b40936649 to your computer and use it in GitHub Desktop.
/// <summary>
/// Exmaple how to go into a Translation Node once it has been created and strip properties
/// based on the document type.
///
/// In this example we remove all but vorto properties - this is actually something TM will do
/// for you on loopback sets from version 2.2 - but the principles show you how to traverse
/// the translation node, go in find values and manipulate them.
/// </summary>
public class EventSetup : ApplicationEventHandler
{
private ProfilingLogger profileLogger;
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
profileLogger = applicationContext.ProfilingLogger;
TranslationNodeService.Created += TranslationNodeService_Created;
}
private void TranslationNodeService_Created(TranslationNodeEventArgs e)
{
using (profileLogger.DebugDuration<EventSetup>("Node Created", "Create Finished"))
{
e.Node.Groups = StripProperties(e.Node, "Our.Umbraco.Vorto").ToList();
}
}
// each TranslationValue can have the value of a list of childValues, each one will have its
// own DataType / Alias and values or even more childValues
private TranslationValue GetStrippedValue(TranslationValue value, params string[] propertiesToKeep)
{
if (propertiesToKeep.InvariantContains(value.EditorAlias)) return value;
if (value.HasChildValues())
{
var newInner = new Dictionary<string, TranslationValue>();
foreach (var innerValue in value.InnerValues)
{
var inner = GetStrippedValue(innerValue.Value, propertiesToKeep);
if (inner != null)
newInner.Add(innerValue.Key, inner);
}
if (newInner.Any())
{
value.InnerValues = newInner;
return value;
}
return null;
}
return null;
}
// starting with the node, we go in the groups (tabs) and go through each TranslationProperty
//
// a TranslationProperty is the property at the toplevel (so the thing defined in the doctype)
// at the top level it has a source and target value
//
// these TranslationValues can contain the value directly (if for example its a textbox) or
// have childValues which also have childvalues (grid/nested content) so you might have to
// recurse down the tree to get to the point you want.
private IEnumerable<TranslationPropertyGroup> StripProperties(TranslationNode node, params string[] propertiesToKeep)
{
var groups = new List<TranslationPropertyGroup>();
foreach(var group in node.Groups)
{
var tab = group;
var properties = new List<TranslationProperty>();
foreach(var property in group.Properties)
{
var sourceValue = GetStrippedValue(property.Source, propertiesToKeep);
var targetValue = GetStrippedValue(property.Source, propertiesToKeep);
if (sourceValue != null)
{
property.Source = sourceValue;
property.Target = targetValue;
properties.Add(property);
}
}
if (properties.Count > 0)
{
tab.Properties = properties;
groups.Add(tab);
}
}
return groups;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment