Skip to content

Commit

Permalink
Merge pull request #5 from jasonmaclean/jm/add-template-config
Browse files Browse the repository at this point in the history
Add items that you would like to render as root items, ignoring their…
  • Loading branch information
jasonmaclean authored Dec 14, 2023
2 parents 670b256 + 2f300d0 commit ba23b87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<RootUrls hint="raw:LoadRootUrls">
<RootUrl sitename="website" language="en" sortorder="1" url="https://your-domain.com" />
</RootUrls>

<!-- Add items that you would like to render as root items, ignoring their place in the IA (https://www.sitecorefundamentals.com/what-to-do-when-a-client-wants-over-500-authorable-redirects) -->
<ItemTypesToTreatAsRootUrls hint="raw:LoadTreatAsRootUrlIds">
<ItemTypesToTreatAsRootUrl>{71710862-98CA-4A76-BE6F-16EBE9FC6F64}</ItemTypesToTreatAsRootUrl>
</ItemTypesToTreatAsRootUrls>

</processor>
</renderContentEditor>
</pipelines>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Feature.PublishedPageUrl.Models;
Expand All @@ -20,6 +21,21 @@ namespace Sitecore.Feature.PublishedPageUrl.Pipelines
public class ShowPublishedPageUrl
{
static List<RootUrl> RootUrls = new List<RootUrl>();
static List<ID> ItemTypesToTreatAsRootUrls { get; set; } = new List<ID>();

public void LoadTreatAsRootUrlIds(XmlNode node)
{
Log.Info($"Sitecore.Feature.PublishedPageUrl.Pipelines.ShowPublishedPageUrl.LoadTreatAsRootUrlIds -> Adding ID {node.InnerText}", this);

try
{
ItemTypesToTreatAsRootUrls.Add(new ID(node.InnerText));
}
catch(Exception ex)
{
Log.Error($"Sitecore.Feature.PublishedPageUrl.Pipelines.ShowPublishedPageUrl.LoadTreatAsRootUrlIds ->Invalid data in node {node.InnerText} ({ex})", this);
}
}

public void LoadRootUrls(XmlNode node)
{
Expand Down Expand Up @@ -69,8 +85,10 @@ public void Process(RenderContentEditorArgs args)
var urlLabel = Settings.GetSetting("Sitecore.Feature.PublishedPageUrl.UrlLabel");
var enableLinkInUrl = Settings.GetBoolSetting("Sitecore.Feature.PublishedPageUrl.EnableLinkInUrl", true);
var dataSectionTitle = Settings.GetSetting("Sitecore.Feature.PublishedPageUrl.DataSectionTitle");

if (ItemHasPresentationDetails(editingItem))

var treatItemAsRootUrl = ItemTypesToTreatAsRootUrls.Any(x => x.Equals(editingItem.TemplateID));

if (ItemHasPresentationDetails(editingItem) || treatItemAsRootUrl)
{
SiteContext itemSite = GetSiteContext(editingItem);

Expand All @@ -80,7 +98,7 @@ public void Process(RenderContentEditorArgs args)
return;
}

RootUrl rootUrl = RootUrls.FirstOrDefault(x => x.SiteName == itemSite.Name && x.Language.ToLowerInvariant() == editingItem.Language.Name.ToLowerInvariant());
var rootUrl = RootUrls.FirstOrDefault(x => x.SiteName == itemSite.Name && x.Language.ToLowerInvariant() == editingItem.Language.Name.ToLowerInvariant());

if (rootUrl != null)
{
Expand All @@ -97,6 +115,23 @@ public void Process(RenderContentEditorArgs args)
var path = LinkManager.GetItemUrl(editingItem, options).ToLowerInvariant();
path = path.Replace(url,"").Replace(":443", "").Replace(":80", "");

if (treatItemAsRootUrl)
{
if (path.StartsWith("/"))
path = path.Substring(1);

var pathParts = path.Split('/');

if (pathParts.Length > 1)
{
path = $"/{path.Split('/').First()}/{path.Split('/').Last()}";
}
else
{
path = $"/{path.Split('/').Last()}";
}
}

url += path;
}
}
Expand Down

0 comments on commit ba23b87

Please sign in to comment.