Last active
March 5, 2019 19:32
-
-
Save weirdpattern/2d25e86f7519778483c21cae5ed53c73 to your computer and use it in GitHub Desktop.
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
| 2019.01.08.field-value-interpolation-sitecore |
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
| /// <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); | |
| } |
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
| /// <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; | |
| } | |
| } |
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
| <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> |
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
| /// <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); | |
| } | |
| } |
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
| /// <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