Last active
February 26, 2018 20:40
-
-
Save digitalParkour/a8f0af237044dfc0f5a775686167cf71 to your computer and use it in GitHub Desktop.
Sitecore Placeholder Toggle Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> [ 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(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment