3

I have made a custom /sitecore/templates/System/Layout/Renderings/View renderingcalled UpdateButton. I would like to only enable this for certain templates. Currently I am completely hiding it when on an item that isn't the appropriate type by setting this.IsVisible = false. This hides the icon completely and makes it so the button cannot be clicked.

Is there a way to set it to be disabled like what can be done with a normal command in the query state where the icon is grayed out and the button cannot be clicked?

Button Code:

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);
        }
    }

    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());
        }
    }
}

Cshtml File:

@using Sitecore.Mvc
@using Common.CustomSitecore.Speak.Controls
@model Sitecore.Mvc.Presentation.RenderingModel
@Html.Sitecore().Controls().UpdateButton(Model.Rendering)

From what I've seen, buttons in core that are disabled at times for the experience editor ribbons still use commands which makes me think this will need to be something custom.

1 Answer 1

0

I needed to add a new js file which had a reference to a request in canExecute, point to it, and add a request for validation that I wanted.

Js:

define(["sitecore", "/-/speak/v1/ExperienceEditor/ExperienceEditor.js"], function (Sitecore, ExperienceEditor) {
    Sitecore.Commands.MyCustomCommand =
    {
        canExecute: function (context) {
            if (!ExperienceEditor.isInMode("edit")) {
                return false;
            }

            return context.app.canExecute("Custom.CanExecute", context.currentContext);
        }
    };
});

Patch for request:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sitecore.experienceeditor.speak.requests>
      <request name="Custom.CanExecute" type="Common.CustomSitecore.Requests.CanExecute, Common"/>
    </sitecore.experienceeditor.speak.requests>
  </sitecore>
</configuration>

CanExecute:

namespace Common.CustomSitecore.Requests.SharedContent
{
    public class CanExecute: PipelineProcessorControlStateRequest<ItemContext>
    {
        public override bool GetControlState()
        {
            return base.RequestContext.Item.InheritsFrom(Constants.MyTemplateID);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.