Making Custom Components not editable in SxA Partials and Page Designs

Recently, I've been having issues with SxA Page Designs and Partials. Granted, I'm not working on a typical SxA implementation, as most of our components are custom built and not out of the box Standard SxA, but I'm not sure what I need to do to them to make them behave properly in Page Designs and Partials.

I've created custom Header and Footer Components, all with their own placeholders and child components, and created an SxA Partial for Header and an SxA Partial for Footer and configured the components in their proper place. I then created a page design to use both of my partials, but it did not quite behave the way I expected.

I expected the components would only be editable on the Page Designs and Partials themselves. Instead, any editable field in my components was editable on any page that used that page design. Not what I wanted to needed and wasn't really sure if I was missing some configuration in my rendering, though I did follow the standard approach for making components "SxA Compliant."

To get around this, I brute forced it, by explicitly setting the editing mode of the page based on the type of page the Context Item was. I added this to the top of my header and footer component cshtml files:

    bool onRegularPage = !Sitecore.Context.Item.IsPartialDesign() && !Sitecore.Context.Item.IsPageDesign();
    var oldDisplayMode = global::Sitecore.Context.Site != null ? global::Sitecore.Context.Site.DisplayMode : Sitecore.Sites.DisplayMode.Normal;
    if (onRegularPage && oldDisplayMode == Sitecore.Sites.DisplayMode.Edit && global::Sitecore.Context.Site != null)
        //disable the editing of the nested component
        global::Sitecore.Context.Site.SetDisplayMode(Sitecore.Sites.DisplayMode.Preview, DisplayModeDuration.Temporary);

Note the "IsPartialDesign()" and the "IsPageDesign()" methods are defined in an Extension method, so you'll need to add using statement to get this to work:

@using Sitecore.XA.Foundation.Presentation.Extensions

Lastly, at the end of your cshtml file, you'll need to make sure you switch back the editing mode to the original one:

    global::Sitecore.Context.Site.SetDisplayMode(oldDisplayMode, DisplayModeDuration.Temporary);

Maybe I messed something else up and there's a configuration based way to get them to behave properly, but the above solution will work if you ever need to force a different editing mode in a component.

Sitecore Foundation - Helix Glass Project