Skip to content

Instantly share code, notes, and snippets.

@weirdpattern
Last active March 5, 2019 19:32
Show Gist options
  • Select an option

  • Save weirdpattern/2d25e86f7519778483c21cae5ed53c73 to your computer and use it in GitHub Desktop.

Select an option

Save weirdpattern/2d25e86f7519778483c21cae5ed53c73 to your computer and use it in GitHub Desktop.
2019.01.08.field-value-interpolation-sitecore
/// <summary>
/// Replaces a variable with a specific value.
/// </summary>
public interface IVariableReplacer
{
/// <summary>
/// Gets the supported variable.
/// </summary>
IEnumerable<string> Variables { get; }
/// <summary>
/// Replaces a value from text
/// </summary>
/// <param name="text">The text to be replaced.</param>
/// <param name="targetItem">The target item being updated.</param>
/// <returns>A string that represents the replaced text.</returns>
string Replace(string text, Item targetItem);
}
/// <summary>
/// Replaces $path with the item path.
/// </summary>
public class PathVariableReplacer : VariableReplacerBase
{
/// <summary>
/// Class constructor.
/// </summary>
public PathVariableReplacer() : base(new string[] { "path" }) { }
/// <inheritdoc />
public override string Replace(string text, Item targetItem)
{
Assert.ArgumentNotNull(text, nameof(text));
Assert.ArgumentNotNull(targetItem, nameof(targetItem));
foreach (var variable in Variables)
{
text = text.Replace(variable, targetItem.Paths.FullPath);
}
return text;
}
}
<configuration xmlns:patch="https://www.sitecore.com/xmlconfig/">
<sitecore>
<pipelines>
<expandInitialFieldValue>
<processor type="MyAssembly.Namespace.Pipelines.ReplaceCustomVariables, MyAssembly.Namespace"
patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']">
<replacers hint="raw:AddVariableReplacer">
<replacer type="MyAssembly.Namespace.VariableReplacers.PathVariableReplacer, MyAssembly.Namespace" />
</replacers>
</processor>
</expandInitialFieldValue>
</pipelines>
</sitecore>
</configuration>
/// <summary>
/// Custom variable replacer.
/// </summary>
public class ReplaceCustomVariables : ExpandInitialFieldValueProcessor
{
/// <summary>
/// Gets the variables to be replaced.
/// </summary>
public List<IVariableReplacer> Replacers { get; } = new List<IVariableReplacer>();
/// <inheritdoc />
public override void Process(ExpandInitialFieldValueArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));
foreach (var replacer in Replacers)
{
if (replacer.Variables.Any(variable => args.SourceField.Value.Contains(variable)))
{
args.Result = replacer.Replace(args.Result, args.TargetItem);
}
}
}
/// <summary>
/// Adds a new variable replacer.
/// </summary>
/// <param name="node">The xml node to be parsed.</param>
public void AddVariableReplacer(XmlNode node)
{
Assert.ArgumentNotNull(node, nameof(node));
var replacer = Factory.CreateObject(node, true) as IVariableReplacer;
Assert.IsNotNull(replacer, "The replacer couldn't be created");
Replacers.Add(replacer);
}
}
/// <summary>
/// Base class for a variable replacer.
/// </summary>
public abstract class VariableReplacerBase : IVariableReplacer
{
/// <inheritdoc />
public IEnumerable<string> Variables { get; } = new List<string>();
/// <summary>
/// Class constructor.
/// </summary>
/// <param name="variables">The variable and aliases to be replaced.</param>
protected VariableReplacerBase(IEnumerable<string> variables)
{
Assert.ArgumentNotNull(variables, nameof(variables));
Assert.IsTrue(variables.Any(), "At least one variable is required");
Variables = variables.Select(variable => StringUtil.EnsurePrefix('$', variable));
}
/// <inheritdoc />
public abstract string Replace(string text, Item targetItem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment