Contextual Ribbons In Page Editor – Part 2

While I was creating another Page Editor ribbon  was hoping to find another way of making the ribbons contextual. I did find an easier way to achieve the same goal, which I will explain here.

In the Core database, in /sitecore/content/Applications/WebEdit/Ribbons/WebEdit, I created a new item based on the /sitecore/templates/System/Ribbon/Strip template.

I set the Type field to my custom class (TestApplication.Commands.Strips.TestStrip).

Here’s the code for the class.

namespace TestApplication.Commands.Strips
{
    using System.Web.UI;
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.Shell.Framework.Commands;
    using Sitecore.Shell.Web.UI.WebControls;
    using Sitecore.Web.UI.WebControls.Ribbons;

    public class TestStrip : RibbonStrip
    {
        public override void Render(HtmlTextWriter output, Ribbon ribbon, Item strip, CommandContext context)
        {
            Assert.ArgumentNotNull(output, "output");
            Assert.ArgumentNotNull(ribbon, "ribbon");
            Assert.ArgumentNotNull(strip, "strip");
            Assert.ArgumentNotNUll(context, "context");

            if (Helper.DescendantOrSelf(context.Items[0], TestConstants.TestID))
            {
                ribbon.RenderChunks(output, strip, context, false);
            }
        }
    }
}

The call to Helper.DescendantOrSelf is a simple test class I created to check whether context.Items[0] is a descendant (or self) of an item with the GUID specified. If that’s the case, the strip will be rendered, otherwise it will not appear.

Easy, no? Sure, it’s theoretically not the same as Contextual Ribbons – you don’t have to set anything in Sitecore, and in this case has hardcoded when to display – but it does do the trick.

While looking for this however, I also noticed (in the Strip item) the ‘Contextual Header’ field. I haven’t been able to find an example of it filled and haven’t yet experimented with it to see how it works. If anyone has any clue or pointers on that, please let me know in the comments.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s