The use-case for this situation is that I want to use the $name of the item to populate a text field for CSS class. Example:
bg-$name
Since this is for a CSS class, I want it to be lowercase, but I'm not sure if this is possible.
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityThe use-case for this situation is that I want to use the $name of the item to populate a text field for CSS class. Example:
bg-$name
Since this is for a CSS class, I want it to be lowercase, but I'm not sure if this is possible.
Yes, but not OOTB. You would need to roll a custom token. Maybe something like $namelower
.
To do this you would extend the ExpandInitialFieldValueProcessor
Here is an example:
using Sitecore.Diagnostics;
using Sitecore.Pipelines.ExpandInitialFieldValue;
namespace CustomExpandTokenProcessors.Pipelines
{
public class CustomTokenReplacer : ExpandInitialFieldValueProcessor
{
private const string Token = "$customtoken";
public override void Process(ExpandInitialFieldValueArgs args)
{
Assert.ArgumentNotNull(args, "args");
args.Result = args.TargetItem.Name.ToLower();
}
}
}
You can add your own code in there to set the result of the token expansion.
Then you would need to patch this in like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<expandInitialFieldValue>
<processor type="CustomExpandTokenProcessors.Pipelines.MyCustomTokenReplacer, CustomExpandTokenProcessors" patch:after="processor[@type='type=Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']"/>
</expandInitialFieldValue>
</pipelines>
</sitecore>
</configuration>
Source of all this goodness is @soren.engel (https://soen.ghost.io/working-with-custom-tokens-in-sitecore/)