Created
November 10, 2020 13:11
-
-
Save auzick/c2e127ed4c6aef0f019eedc0ab738d2b to your computer and use it in GitHub Desktop.
Sitecore SXA Scriban extension to expose the innards of a link field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/"> | |
<sitecore> | |
<pipelines> | |
<generateScribanContext> | |
<processor type = "Example.ScribanExtensions.Pipelines.LinkExtensions, Example.ScribanExtensions" resolve="true" /> | |
</generateScribanContext> | |
</pipelines> | |
</sitecore> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Scriban.Runtime; | |
using Sitecore.Data.Fields; | |
using Sitecore.Data.Items; | |
using Sitecore.XA.Foundation.Abstractions; | |
using Sitecore.XA.Foundation.Scriban.Pipelines.GenerateScribanContext; | |
using System; | |
using Example.ScribanExtensions.Model; | |
namespace Example.ScribanExtensions.Pipelines | |
{ | |
public class LinkExtensions : IGenerateScribanContextProcessor | |
{ | |
protected readonly IPageMode PageMode; | |
private readonly IContext context; | |
private delegate LinkInfo LinkInfoDelegate(Item item, object linkFieldName); | |
public LinkExtensions(IPageMode pageMode, IContext context) | |
{ | |
PageMode = pageMode; | |
this.context = context; | |
} | |
public void Process(GenerateScribanContextPipelineArgs args) | |
{ | |
var linkInfo = new LinkInfoDelegate(GetLinkInfo); | |
args.GlobalScriptObject.Import("sc_link_info", (Delegate)linkInfo); | |
} | |
public LinkInfo GetLinkInfo(Item item, object field) | |
{ | |
if (item == null | |
|| String.IsNullOrWhiteSpace((string)field) | |
|| item.Fields[(string)field] == null | |
) | |
{ | |
return null; | |
} | |
LinkField lnkField = (LinkField)item.Fields[(string)field]; | |
if (lnkField == null) | |
{ | |
return null; | |
} | |
return new LinkInfo(lnkField); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.Data.Fields; | |
namespace Example.ScribanExtensions.Model | |
{ | |
public class LinkInfo | |
{ | |
public string url { get; set; } | |
public string text { get; set; } | |
public string title { get; set; } | |
public string anchor { get; set; } | |
public string target { get; set; } | |
public LinkInfo() { } | |
public LinkInfo(LinkField item) | |
{ | |
url = item.GetFriendlyUrl(); | |
text = item.Text; | |
title = item.Title; | |
anchor = item.Anchor; | |
target = item.Target; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment