Experience Editor command token replacer

When making Experience Editor buttons usually we use something like a field editor which has it’s own token functionality to swap out parameters like datasource ID and field names.  however if you’re planning something more generic, like running a bit of javascript then it comes in handy to have the ability to replace your own tokens.

GetChromeData pipeline

Before the Experience Editor context editing ribbon is rendered it runs the GetChromeData pipeline, we can easily patch into this pipeline to replace tokens in the edit button’s click text.

In this example i’m simply replacing the token $id with the ID of of the datasource item that this ribbon button exists on.  Very simple yet also quite powerful.

public class CommandTokenReplacer : GetChromeDataProcessor
{
	public override void Process(GetChromeDataArgs args)
	{
		foreach (var webEditButton in args.ChromeData.Commands)
		{
			webEditButton.Click = webEditButton.Click.Replace("$id", args.Item.ID.ToString());
		}
	}
}