sitecorecontent-editor

Sitecore - how to show templates under specific folder, inside insert options context menu


Lets say the user itself is able to add new branch templates.

And on a homepage item, insert options must consist of items inside that branch templates folder.

In sitecore, insert options can only be set to specific items. When I select a folder as an insert option, sitecore shows that folder item (which is perfectly normal).

I need to make something like either showing items dynamically inside a specific folder, or setting the starting path of insert options browsing dialog.

Is any of this possible ?


Solution

  • Blog post: https://sitecorealekseyshevchenko.wordpress.com/2017/09/19/dynamic-insert-options/

    Create 'Dynamic Insert Option' template wich contains the only field 'Starting Path' type of 'Droptree' and source value is '{3C1715FE-6A13-4FCF-845F-DE308BA9741D}' - id of '/sitecore/templates' item.

    Then add 'Dynamic Insert Option' template to list of templates in 'Base template' field of the template which should have dynamic insert options.

    enter image description here

    Patch 'uiGetMasters' processor with such config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <processors>
          <uiGetMasters>
            <processor mode="on"
                       type="DynamicInsertOption.Processors.GetDynamicInsertOption, DynamicInsertOption"
                       patch:before="processor[@type='Sitecore.Pipelines.GetMasters.CheckSecurity, Sitecore.Kernel']" />
          </uiGetMasters>
        </processors>
      </sitecore>
    </configuration>
    

    Implement GetDynamicInsertOption processor:

    namespace DynamicInsertOption.Processors
    {
        using Sitecore.Data.Items;
        using Sitecore.Diagnostics;
        using Sitecore.Pipelines.GetMasters;
    
        public class GetDynamicInsertOption
        {
            public void Process(GetMastersArgs args)
            {
                Assert.ArgumentNotNull(args, "args");
    
                var startingPath = args.Item["Starting Path"];
    
                if (!string.IsNullOrEmpty(startingPath))
                {
                    for (int i = args.Masters.Count - 1; i > -1; i--) { args.Masters.RemoveAt(i); }
    
                    var startingFolder = args.Item.Database.GetItem(startingPath);
    
                    foreach (Item master in startingFolder.Children) { args.Masters.Add(master); }
                }
            }
        }
    }
    

    The result see on pic below:

    enter image description here