Expanding on this question I wanted to make the template id configurable so I can have different buttons active for different templates without duplicating that code. I created a new button in Sitecore that inherits from /sitecore/templates/System/Ribbon/Large Button
which has a field called Enabled On Template
but I can't find a way to pass this through the javascript and into the class that inherits from PipelineProcessorControlStateRequest<ItemContext>
. I tried overriding the custom button's AddAttributes method and added the field's value as an attribute however it is not available in context.button.attributes
in the canExecute
method of my javascript. Is there a built in way to pass values from the button's item to a pipeline processor control state request?
Here's what I tried that didn't work:
namespace Common.CustomSitecore.Speak.Controls
{
public class UpdateButton : Sitecore.ExperienceEditor.Speak.Ribbon.Controls.LargeButton.LargeButton
{
public UpdateButton(RenderingParametersResolver renderingParametersResolver) : base(renderingParametersResolver)
{
}
protected override void InitializeControl(RenderingParametersResolver resolver)
{
if(ResolveContentItem()?.TemplateId != "{MyId}")
this.IsVisible = false;
base.InitializeControl(resolver);
}
protected override void AddAttributes(HtmlTextWriter output)
{
//I also tried prefixing this with "data-sc-" because the html for
//attributes that the button ends up having seem to have that
output.AddAttribute("EnabledOnTemplate", DataSourceItem[Constants.EnabledOnTemplateFieldId]);
base.AddAttributes(output);
}
}
public static class ControlsExtension
{
public static HtmlString UpdateButton(this Sitecore.Mvc.Controls controls, Rendering rendering)
{
Sitecore.Diagnostics.Assert.ArgumentNotNull(controls, "controls");
Sitecore.Diagnostics.Assert.ArgumentNotNull(rendering, "rendering");
var largeButton = new UpdateButton(controls.GetParametersResolver(rendering));
return new HtmlString(largeButton.Render());
}
}
}
Js:
define(["sitecore", "/-/speak/v1/ExperienceEditor/ExperienceEditor.js"], function (Sitecore, ExperienceEditor) {
Sitecore.Commands.MyCustomCommand =
{
canExecute: function (context) {
if (!ExperienceEditor.isInMode("edit")) {
return false;
}
//Here I wanted to read the attribute I added however it does not
//seem to be attached to the button. After that I was going to create a
//new item context to use the with the request but since I can't even
//seem to read it here I think this approach is a dead end because
//context.button.attributes.EnabledOnTemplate is undefined
context.currentContext.EnabledOnTemplate = context.button.attributes.EnabledOnTemplate;
return context.app.canExecute("Custom.CanExecute", context.currentContext);
}
};
});