Skip to content

Instantly share code, notes, and snippets.

@digitalParkour
Last active February 26, 2018 20:40
Show Gist options
  • Select an option

  • Save digitalParkour/a8f0af237044dfc0f5a775686167cf71 to your computer and use it in GitHub Desktop.

Select an option

Save digitalParkour/a8f0af237044dfc0f5a775686167cf71 to your computer and use it in GitHub Desktop.
Sitecore Placeholder Toggle Snippets
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<!-- :: Only applicable to CA environments :: -->
<!-- this skips placeholder (Header and Footer) in EE but can be toggled back on with a button in the EE ribbon -->
<sitecore>
<pipelines>
<mvc.renderPlaceholder>
<!-- Patch to be first processor -->
<processor type="Community.Foundation.Optimizations.Pipelines.MvcRenderPlaceholder.SkipPlaceholdersByUserSetting, Community.Foundation.Optimizations"
patch:before="*[1]"/>
</mvc.renderPlaceholder>
</pipelines>
</sitecore>
</configuration>
<r><d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}"><r id="{BDE651C9-7988-4950-8E01-EA80106563A2}" uid="{C7A756BA-37FA-4A10-826A-D4C5911013F5}" par="Tooltip&amp;IsVisible=1&amp;Click=trigger%3Abutton%3Acheck&amp;Command=ToggleHeaderFooter&amp;ControlStateRequest&amp;PostponedCall&amp;AccessKey&amp;ConfigurationItem&amp;IsEnabled&amp;Id&amp;Behaviors&amp;PageCodeScriptFileName=%2Fsitecore%2Fshell%2Fclient%2FSitecore%2FCustom%2FExperienceEditor%2FCommands%2FToggleHeaderFooter.js&amp;RegistryKey=%2FCurrent_User%2FPage%20Editor%2FShow%2Fheaderfooter&amp;QueryKey" /></d></r>
using Sitecore.Mvc.Pipelines.Response.RenderPlaceholder;
using System.Linq;
namespace Community.Foundation.Optimizations.Pipelines.MvcRenderPlaceholder
{
/// <summary>
/// This processor is called for every placeholder that Sitecore renders.
/// We will step in and abort processing for a placeholder if user toggled it off.
/// </summary>
public class SkipPlaceholdersByUserSetting : RenderPlaceholderProcessor
{
// List placeholder names that are hide-able
// This could be done with a custom sitecore config
static readonly string[] ApplicablePlaceholders = new string[] { "header", "footer"};
public override void Process(RenderPlaceholderArgs args)
{
// Only applies to Experience Editor
if (Sitecore.Context.PageMode.IsNormal)
return;
// Ignore if this placeholder is not hide-able
if (!ApplicablePlaceholders.Contains(args.PlaceholderName))
return;
// Only applies to known user
var profile = Sitecore.Context.User?.Profile;
if (profile == null)
return;
// Match our checkbox registry key
var registryKey = $"/{profile.ProfileUser.Name}/Page Editor/Show/headerfooter";
var hide = profile[registryKey] != "on";
// Exit if nothing to do... user wants to render placeholder as normal
if (!hide)
return;
// Add a friendly message to authors so it doesn't seem broken
var styles = "font-size: 1.5em; vertical-align: middle; padding: 0px 4px 0px 4px; top: -1px; position: relative;";
var msg = $"<div><br /><p>&nbsp;&nbsp;&nbsp;[ Skipped <b style=\"{styles}\">{args.PlaceholderName}</b> placeholder. Toggle Header/Footer in ribbon above. ]</p><br /></div>";
args.Writer.Write(msg);
// Abort any further processing for this placeholder
args.AbortPipeline();
}
}
}
define(["sitecore", "/-/speak/v1/ExperienceEditor/ExperienceEditor.js"], function (Sitecore, ExperienceEditor) {
Sitecore.Commands.ToggleHeaderFooter =
{
canExecute: function (context) {
return true;
},
execute: function (context) {
ExperienceEditor.PipelinesUtil.generateRequestProcessor(
"ExperienceEditor.ToggleRegistryKey.Toggle",
function (response) {
window.top.location.reload(); // always reload on click
},
{
value: context.button.get("registryKey") // link is checked to registry key value
}
).execute(context);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment