Sitecore SXA available renderings
We have a multisite setup in SXA to easily create (small) websites and have been sharing a lot of configuration, layout information and content between those. Check my latest SXA blog posts for more information.
We did already cover sharing placeholder settings. But if we can share those, we might as well share the available renderings configuration. This is a list of renderings that is defined per site to define the availability within the SXA Toolbox - and since SXA 1.8 we can also define the structure of the toolbox with these Available renderings. As explained in the documentation, you have to check the box "Group renderings in sections according to Available Renderings items in the site" to do so.
I believe it is a best practice to do this so that feature is always on in my projects.
But let's get back to sharing... We have a shared site (and master site) setup, so it would be a good idea to define the available renderings in the shared site and not in every site (created from the master site). This idea was on my radar and I got triggered by Toby Gutierrez to really try this now.
Sharing available renderings
The toolbox
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.DependencyInjection;
using Sitecore.XA.Foundation.Editing.Models;
using Sitecore.XA.Foundation.Multisite;
using Sitecore.XA.Foundation.Presentation;
using Sitecore.XA.Foundation.SitecoreExtensions.Extensions;
using Sitecore.XA.Foundation.SitecoreExtensions.Repositories;
namespace Feature.Multisite.Editing
{
public class AvailableRenderingsOrderingService : Sitecore.XA.Foundation.Editing.Service.AvailableRenderingsOrderingService
{
public AvailableRenderingsOrderingService(
IPresentationContext presentationContext,
IMultisiteContext multisiteContext,
IContentRepository contentRepository) : base(presentationContext, multisiteContext, contentRepository)
{
}
public override IList<AvailableRenderingEntry> GetOrderedRenderings(Item siteItem, IList<Item> renderings = null)
{
var orderDictionary = new Dictionary<ID, ValueTuple<int, string>>();
var presentationItem = PresentationContext.GetPresentationItem(siteItem);
var folder = presentationItem?.FirstChildInheritingFrom(Sitecore.XA.Foundation.Presentation.Templates.AvailableRenderingsFolder.ID);
if (folder != null)
{
if (!folder.Children.Any())
{
var shared = ServiceLocator.ServiceProvider.GetService<ISharedSitesContext>().GetSharedSitesWithoutCurrent(siteItem);
foreach (var site in shared)
{
presentationItem = PresentationContext.GetPresentationItem(site);
folder = presentationItem?.FirstChildInheritingFrom(Sitecore.XA.Foundation.Presentation.Templates.AvailableRenderingsFolder.ID);
if (folder != null && folder.Children.Any())
{
break;
}
}
}
if (folder != null)
{
var sections = (CheckboxField)folder.Fields[Sitecore.XA.Foundation.Presentation.Templates.AvailableRenderingsFolder.Fields.GroupRenderingsInSections];
if (sections != null && sections.Checked)
{
var array = folder.Children.Where(item => !string.IsNullOrEmpty(item[Sitecore.XA.Foundation.Presentation.Templates._RenderingsList.Fields.Renderings])).ToArray();
for (var index1 = 0; index1 < array.Length; ++index1)
{
var fieldRenderings = (MultilistField)array[index1].Fields[Sitecore.XA.Foundation.Presentation.Templates._RenderingsList.Fields.Renderings];
for (var index2 = 0; index2 < fieldRenderings.TargetIDs.Length; ++index2)
{
if (!orderDictionary.ContainsKey(fieldRenderings.TargetIDs[index2]))
{
orderDictionary.Add(fieldRenderings.TargetIDs[index2], new ValueTuple<int, string>((index1 + 1) * 1000 + index2, fieldRenderings.InnerField.Item.DisplayName));
}
}
}
}
}
}
return renderings?.Select(r => new AvailableRenderingEntry
{
RenderingItem = r,
Order = orderDictionary.ContainsKey(r.ID) ? orderDictionary[r.ID].Item1 : 0,
SectionName = orderDictionary.ContainsKey(r.ID) ? orderDictionary[r.ID].Item2 : r.Parent.DisplayName
}).OrderBy(r => r.Order).ToList();
}
}
}
<sitecore>
<services>
<register serviceType="Sitecore.XA.Foundation.Editing.Service.IAvailableRenderingsOrderingService, Sitecore.XA.Foundation.Editing"
implementationType="Sitecore.XA.Foundation.Editing.Service.AvailableRenderingsOrderingService, Sitecore.XA.Foundation.Editing" lifetime="Singleton">
<patch:attribute name="implementationType">Feature.Multisite.Editing.AvailableRenderingsOrderingService, Feature.Multisite</patch:attribute>
</register>
</services>
</sitecore>
No comments:
Post a Comment