Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SitecoreHelper
- {
- public static HtmlString DynamicPlaceholder(this Sitecore.Mvc.Helpers.SitecoreHelper helper, string dynamicKey)
- {
- var currentRenderingId = RenderingContext.Current.Rendering.UniqueId;
- return helper.Placeholder(string.Format("{0}_{1}", dynamicKey, currentRenderingId));
- }
- }
- /// <summary>
- /// Handles changing context to the references dynamic "master" renderings settings for inserting the allowed controls for the placeholder and making it editable
- /// </summary>
- public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
- {
- //text that ends in a GUID
- private const string DYNAMIC_KEY_REGEX = @"(.+)_[dw]{8}-([dw]{4}-){3}[dw]{12}";
- public new void Process(GetPlaceholderRenderingsArgs args)
- {
- Assert.IsNotNull(args, "args");
- string placeholderKey = args.PlaceholderKey;
- Regex regex = new Regex(DYNAMIC_KEY_REGEX);
- Match match = regex.Match(placeholderKey);
- if (match.Success && match.Groups.Count > 0)
- {
- placeholderKey = match.Groups[1].Value;
- }
- else
- {
- return;
- }
- // Same as Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings but with fake placeholderKey
- Item placeholderItem = null;
- if (ID.IsNullOrEmpty(args.DeviceId))
- {
- placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
- args.LayoutDefinition);
- }
- else
- {
- using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
- {
- placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
- args.LayoutDefinition);
- }
- }
- List<Item> collection = null;
- if (placeholderItem != null)
- {
- bool flag;
- args.HasPlaceholderSettings = true;
- collection = this.GetRenderings(placeholderItem, out flag);
- if (flag)
- {
- args.CustomData["allowedControlsSpecified"] = true;
- args.Options.ShowTree = false;
- }
- }
- if (collection != null)
- {
- if (args.PlaceholderRenderings == null)
- {
- args.PlaceholderRenderings = new List<Item>();
- }
- args.PlaceholderRenderings.AddRange(collection);
- }
- }
- }
- /// <summary>
- /// Replaces the Displayname of the Placeholder rendering with the dynamic "parent"
- /// </summary>
- public class GetDynamicPlaceholderChromeData : GetChromeDataProcessor
- {
- //text that ends in a GUID
- private const string DYNAMIC_KEY_REGEX = @"(.+)_[dw]{8}-([dw]{4}-){3}[dw]{12}";
- public override void Process(GetChromeDataArgs args)
- {
- Assert.ArgumentNotNull(args, "args");
- Assert.IsNotNull(args.ChromeData, "Chrome Data");
- if ("placeholder".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
- {
- string argument = args.CustomData["placeHolderKey"] as string;
- string placeholderKey = argument;
- Regex regex = new Regex(DYNAMIC_KEY_REGEX);
- Match match = regex.Match(placeholderKey);
- if (match.Success && match.Groups.Count > 0)
- {
- // Is a Dynamic Placeholder
- placeholderKey = match.Groups[1].Value;
- }
- else
- {
- return;
- }
- // Handles replacing the displayname of the placeholder area to the master reference
- Item item = null;
- if (args.Item != null)
- {
- string layout = ChromeContext.GetLayout(args.Item);
- item = Sitecore.Client.Page.GetPlaceholderItem(placeholderKey, args.Item.Database, layout);
- if (item != null)
- {
- args.ChromeData.DisplayName = item.DisplayName;
- }
- if ((item != null) && !string.IsNullOrEmpty(item.Appearance.ShortDescription))
- {
- args.ChromeData.ExpandedDisplayName = item.Appearance.ShortDescription;
- }
- }
- }
- }
- }
- <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
- <sitecore>
- <pipelines>
- <getPlaceholderRenderings>
- <processor type="MyCustomNamespace.Pipelines.GetPlaceholderRenderings.GetDynamicKeyAllowedRenderings,MyCustomNamespace" patch:before="processor[@type='Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings,Sitecore.Kernel']"/>
- </getPlaceholderRenderings>
- <getChromeData>
- <processor type="Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel">
- <patch:attribute name="type">MyCustomNamespace.Pipelines.GetPlaceholderRenderings.GetDynamicPlaceholderChromeData,MyCustomNamespace</patch:attribute>
- </processor>
- </getChromeData>
- </pipelines>
- </sitecore>
- </configuration>
- <getChromeData>
- <processor type="MyCustomNamespace.Pipelines.GetChromeData.GetDynamicPlaceholderChromeData,MyCustomNamespace" patch:after="processor[@type='Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel']"/>
- </getChromeData>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement