Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. public static class SitecoreHelper
  2. {
  3. public static HtmlString DynamicPlaceholder(this Sitecore.Mvc.Helpers.SitecoreHelper helper, string dynamicKey)
  4. {
  5. var currentRenderingId = RenderingContext.Current.Rendering.UniqueId;
  6. return helper.Placeholder(string.Format("{0}_{1}", dynamicKey, currentRenderingId));
  7. }
  8. }
  9.  
  10. /// <summary>
  11. /// Handles changing context to the references dynamic "master" renderings settings for inserting the allowed controls for the placeholder and making it editable
  12. /// </summary>
  13. public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
  14. {
  15. //text that ends in a GUID
  16. private const string DYNAMIC_KEY_REGEX = @"(.+)_[dw]{8}-([dw]{4}-){3}[dw]{12}";
  17.  
  18. public new void Process(GetPlaceholderRenderingsArgs args)
  19. {
  20. Assert.IsNotNull(args, "args");
  21.  
  22. string placeholderKey = args.PlaceholderKey;
  23. Regex regex = new Regex(DYNAMIC_KEY_REGEX);
  24. Match match = regex.Match(placeholderKey);
  25. if (match.Success && match.Groups.Count > 0)
  26. {
  27. placeholderKey = match.Groups[1].Value;
  28. }
  29. else
  30. {
  31. return;
  32. }
  33. // Same as Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings but with fake placeholderKey
  34. Item placeholderItem = null;
  35. if (ID.IsNullOrEmpty(args.DeviceId))
  36. {
  37. placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
  38. args.LayoutDefinition);
  39. }
  40. else
  41. {
  42. using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
  43. {
  44. placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
  45. args.LayoutDefinition);
  46. }
  47. }
  48. List<Item> collection = null;
  49. if (placeholderItem != null)
  50. {
  51. bool flag;
  52. args.HasPlaceholderSettings = true;
  53. collection = this.GetRenderings(placeholderItem, out flag);
  54. if (flag)
  55. {
  56. args.CustomData["allowedControlsSpecified"] = true;
  57. args.Options.ShowTree = false;
  58. }
  59. }
  60. if (collection != null)
  61. {
  62. if (args.PlaceholderRenderings == null)
  63. {
  64. args.PlaceholderRenderings = new List<Item>();
  65. }
  66. args.PlaceholderRenderings.AddRange(collection);
  67. }
  68. }
  69. }
  70.  
  71. /// <summary>
  72. /// Replaces the Displayname of the Placeholder rendering with the dynamic "parent"
  73. /// </summary>
  74. public class GetDynamicPlaceholderChromeData : GetChromeDataProcessor
  75. {
  76. //text that ends in a GUID
  77. private const string DYNAMIC_KEY_REGEX = @"(.+)_[dw]{8}-([dw]{4}-){3}[dw]{12}";
  78.  
  79. public override void Process(GetChromeDataArgs args)
  80. {
  81. Assert.ArgumentNotNull(args, "args");
  82. Assert.IsNotNull(args.ChromeData, "Chrome Data");
  83. if ("placeholder".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
  84. {
  85. string argument = args.CustomData["placeHolderKey"] as string;
  86.  
  87. string placeholderKey = argument;
  88. Regex regex = new Regex(DYNAMIC_KEY_REGEX);
  89. Match match = regex.Match(placeholderKey);
  90. if (match.Success && match.Groups.Count > 0)
  91. {
  92. // Is a Dynamic Placeholder
  93. placeholderKey = match.Groups[1].Value;
  94. }
  95. else
  96. {
  97. return;
  98. }
  99.  
  100. // Handles replacing the displayname of the placeholder area to the master reference
  101. Item item = null;
  102. if (args.Item != null)
  103. {
  104. string layout = ChromeContext.GetLayout(args.Item);
  105. item = Sitecore.Client.Page.GetPlaceholderItem(placeholderKey, args.Item.Database, layout);
  106. if (item != null)
  107. {
  108. args.ChromeData.DisplayName = item.DisplayName;
  109. }
  110. if ((item != null) && !string.IsNullOrEmpty(item.Appearance.ShortDescription))
  111. {
  112. args.ChromeData.ExpandedDisplayName = item.Appearance.ShortDescription;
  113. }
  114. }
  115. }
  116. }
  117. }
  118.  
  119. <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  120. <sitecore>
  121. <pipelines>
  122. <getPlaceholderRenderings>
  123. <processor type="MyCustomNamespace.Pipelines.GetPlaceholderRenderings.GetDynamicKeyAllowedRenderings,MyCustomNamespace" patch:before="processor[@type='Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings,Sitecore.Kernel']"/>
  124. </getPlaceholderRenderings>
  125. <getChromeData>
  126. <processor type="Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel">
  127. <patch:attribute name="type">MyCustomNamespace.Pipelines.GetPlaceholderRenderings.GetDynamicPlaceholderChromeData,MyCustomNamespace</patch:attribute>
  128. </processor>
  129. </getChromeData>
  130. </pipelines>
  131. </sitecore>
  132. </configuration>
  133.  
  134. <getChromeData>
  135. <processor type="MyCustomNamespace.Pipelines.GetChromeData.GetDynamicPlaceholderChromeData,MyCustomNamespace" patch:after="processor[@type='Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel']"/>
  136. </getChromeData>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement