Sitecore comes with several standard custom value tokens when creating branch templates (i.e. $name
for the name of the new item, $parentid
, for the id of the parent).
Is there anyway to add new variables?
Specifically I want a variable that will allow me to access the items path when added?
There is a sitecore blog post for this ADD CUSTOM STANDARD VALUES TOKENS IN THE SITECORE ASP.NET CMS but TBH, it's wrong . I'm not sure why sitecore insist on producing "untested prototype(s)" all the time in these posts. The guy in that blog literally says You can implement a solution based on the following untested prototype o_O
For some reason sitecore are jumping though various hoops to decompile the source and then recreate it (give a man a hammer and everything looks like a nail maybe?). This makes your code very fragile should the default behaviour change and is just totally unnecessary.
You can add a new variable in a few lines of code:
public class NewVariablesReplacer : MasterVariablesReplacer
{
public override string Replace(string text, Item targetItem)
{
//still need to assert these here
Sitecore.Diagnostics.Assert.ArgumentNotNull(text, "text");
Sitecore.Diagnostics.Assert.ArgumentNotNull(targetItem, "targetItem");
string tempTxt = text;
if (text.Contains("$path"))
{
Sitecore.Diagnostics.Assert.ArgumentNotNull(targetItem.Paths, "targetItem.Paths");
Sitecore.Diagnostics.Assert.ArgumentNotNull(targetItem.Paths.FullPath, "targetItem.Paths.FullPath");
tempTxt = text.Replace("$path", targetItem.Paths.FullPath);
}
//Do what you would normally do.
return base.Replace(tempTxt, targetItem);
}
}
This works without decompiling because it retains the base functionality by calling base.Replace(text, targetItem);
.
You then need to alter the default behaviour in the xml as in the blog post:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<settings>
<setting name="MasterVariablesReplacer">
<patch:attribute name="value">Sitecore.Sharedsource.Data.NewVariablesReplacer ,Sitecore.Sharedsource</patch:attribute>
</setting>
</settings>
</sitecore>
</configuration>