Referencing folders with Placeholder Settings
Placeholder Settings allow you to choose what Renderings / Sublayouts you want to be selectable to add in to the placeholders of your layouts.
Each time you create a new Rendering, you need to update the placeholder settings too so that the editors can use the rendering. If you keep your renderings nicely organised within a folder structure, then there’s a simple method for making this a bit more flexible and having Sitecore automatically pick up the renderings by referencing a folder of renderings rather than the rendering itself.
Within the Content Editor for the settings, Sitecore allows you to add a rendering folder, rather than a rendering itself:
Typically, selecting the folder would have no effect. However, with the following pipeline, we can alter the functionality of Sitecore so that if a folder has been added, it pulls in all of the child renderings that sit underneath it.
This can be very useful if you keep your renderings organised by functionality, so for example you want a “Forms” placeholders setting that will contain all of the Form renderings you have created. If you keep them together in the same folder, you can have them added automatically, without having to update the settings.
Creating the pipeline class
The following is all the code we need for the pipeline step:
|
|
The code for this is relatively straight-forward. It makes use of a standard DerivesFromTemplate
extension method that you’ll need to implement. There is also a static class used for ID references, but you could just drop the IDs straight in.
The GetChildRenderings method is actually extremely simple and could be removed, but it’s left in to illustrate that you could replace this with other logic, such as all descendants of an item, or perhaps using a parameter to determine what renderings to pick up.
Adding the config
With this code in our solution, we need to add the command to config so that it can be referenced. This can done with a simple config file in the Include folder, with the appropriate assembly + class reference:
|
|
And that’s it! You can now just reference folders in your Placeholder Settings and all of the child renderings underneath will be pulled in.