custom standard value tokens
Sitecore provides the ability to use a number of tokens, set on a templates Standard Values, that will be replaced during item creation. Further information and a list of available tokens can be found here
In order to implement custom tokens, we need to implement a custom ExpandInitialFieldValueProcessor
; and then patch that processor via configuration.
This is example is going to provide a $path
token and replace it with the full (relative to the website route) path.
Processor C# File:
public class PathStandardValueProcessor : ExpandInitialFieldValueProcessor
{
private const string _pathToken = "$path";
private const string _basePath = "/sitecore/content/{website_name}/Home/";
public override void Process(ExpandInitialFieldValueArgs args)
{
if (args.SourceField.Value.Contains(_pathToken))
{
if (args.TargetItem != null && args.TargetItem.Parent != null)
{
var fullPath = args.TargetItem.Paths.FullPath;
var sitePath = fullPath.Replace(_basePath, string.Empty);
args.Result = args.Result.Replace(_pathToken, sitePath);
}
}
}
}
Custom Configuration Patch File:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>
<pipelines>
<expandInitialFieldValue>
<processor type="namespace.PathStandardValueProcessor, Project.Name" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']" />
</expandInitialFieldValue>
</pipelines>
</sitecore>
</configuration>