williamsk / SitecorePlaceholderSettingsRules

Adds a rules engine field to placeholder settings items in Sitecore.
4 stars 3 forks source link

Sitecore placeholder settings rules with dynamic key placeholder #1

Open selketjah opened 11 years ago

selketjah commented 11 years ago

Hi

I have installed your package within a sitecore 7.0. With the normal sitecore placeholder, everything works great! (thx btw)

without_pipeline

But I also use dynamic key placeholders within my project. After installing the package, in the page editor mode, the allowed controls are no longer pre selected as with the normal sitecore placeholders. with_pipeline

could you please assist me?

Thank you!

With kind regards Verschatse Gien

williamsk commented 11 years ago

Thanks for the report!

Which flavor of dynamic placeholder are you using? (I've seen two or three different solutions). I haven't tested this with either Sitecore 7 or dynamic placeholders yet, but would love to investigate.

Thanks!

selketjah commented 11 years ago

Hi

thank you for the quick response!

I used a blog post on dynamic placeholders and adjust it to my specific needs. I attached my class.

If I can be of any assistance, please do not hesitate to contact me.

2013/10/28 williamsk notifications@github.com

Thanks for the report!

Which flavor of dynamic placeholder are you using? (I've seen two or three different solutions). I haven't tested this with either Sitecore 7 or dynamic placeholders yet, but would love to investigate.

Thanks!

— Reply to this email directly or view it on GitHubhttps://github.com/williamsk/SitecorePlaceholderSettingsRules/issues/1#issuecomment-27210174 .

using System.Collections.Generic; using System.Linq; using System.Web.UI; using Sitecore.Common; using Sitecore.Layouts; using Sitecore.Web.UI; using Sitecore.Web.UI.WebControls;

namespace Web.Controls { public class DynamicKeyPlaceholder : WebControl, IExpandable { protected string _key = Placeholder.DefaultPlaceholderKey; protected string _dynamicKey = null; protected Placeholder _placeholder;

    public Placeholder InnerPlaceholder
    {
        get { return _placeholder; }
    }

    public string Key 
    {
        get { return _key; }
        set { _key = value.ToLower(); }
    }

    protected string DynamicKey {
        get {
            if (_dynamicKey != null) 
            { 
                return _dynamicKey; 
            }
            _dynamicKey = _key; 
            //find the last placeholder processed, will help us find our parent 
            var stack = Switcher<Placeholder, PlaceholderSwitcher>.GetStack(false); 
            if (stack == null || stack.Count == 0)
            {
                //not used within a placeholder apparently. dynamic key is actually not necessary in this case. 
                return _dynamicKey;
            }
            var current = stack.Peek(); 
            //find the rendering reference we are contained in 
            var renderings = Sitecore.Context.Page.Renderings.Where(rendering => (rendering.Placeholder == current.ContextKey || rendering.Placeholder == current.Key) && rendering.AddedToPage);
            var renderingReferences = renderings as IList<RenderingReference> ?? renderings.ToList();
            if (renderingReferences.Any()) 
            {
                //last one added to page defines our parent 
                var rendering = renderingReferences.Last();
                //use rendering reference unique ID to uniquely and permanently identify the placeholder 
                _dynamicKey = _key + rendering.UniqueId; 
            }
            return _dynamicKey; 
        } 
    } 

    protected override void CreateChildControls() 
    {
        Sitecore.Diagnostics.Tracer.Debug("DynamicKeyPlaceholder: Adding dynamic placeholder with Key" + DynamicKey);
        _placeholder = new Placeholder {Key = this.DynamicKey};
        this.Controls.Add(_placeholder);
        _placeholder.Expand();
    }

    protected override void DoRender(HtmlTextWriter output) 
    {
        base.RenderChildren(output);
    }

    #region IExpandable Members 
    public void Expand()
    {
        this.EnsureChildControls(); 
    }
    #endregion 
}

}