As explained in the previous blog about Scribans - Sitecore SXA 9.3, a major feature of Sitecore SXA 9.3 is the scribans and the best way to use it is with Rendering Variants.
Sitecore 9.3 provides various new things with the introduction of each version, but the best part of it is to extend those features. Today we are going to extend the scriban`s functionality by adding our custom scriban and we will add a new Member for the scriban .
CODE
public class GetLinkUrl : GetScribanItemMember
{
private readonly IContext context;
protected override string MemberName => "linkurl";
public GetLinkUrl(IContext context)
{
this.context = context;
}
protected override void Resolve(GetScribanItemMembersPipelineArgs args)
{
if(args.Mode == MemberMode.Name)
{
args.Members.Add(MemberName);
}
else
{
var field = args.Item.Fields["{EE230482-43D4-4144-9EA4-CD33494AC364}"];
var fieldType = FieldTypeManager.GetField(field);
var linkfieldUrl = "";
if(fieldType is LinkField)
{
linkfieldUrl = LinkUrl(field);
}
args.MemberValue = (object)linkfieldUrl;
}
}
public static string LinkUrl(LinkField linkField)
{
if (linkField != null)
{
switch (linkField.LinkType.ToLower())
{
case "internal":
// Use LinkMananger for internal links, if link is not empty
return linkField.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(linkField.TargetItem) : string.Empty;
case "media":
// Use MediaManager for media links, if link is not empty
return linkField.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(linkField.TargetItem) : string.Empty;
case "external":
// Just return external links
return linkField.Url;
case "anchor":
// Prefix anchor link with # if link if not empty
return !string.IsNullOrEmpty(linkField.Anchor) ? "#" + linkField.Anchor : string.Empty;
case "mailto":
// Just return mailto link
return linkField.Url;
case "javascript":
// Just return javascript
return linkField.Url;
default:
// Just please the compiler, this
// condition will never be met
return linkField.Url;
}
}
return "";
}
}
The GetLinkUrl class is inheriting from the GetScribanItemMember class and then we added a new Member in the scriban using the MemberName property and we are then checking if the field type is a link field then get the URL from the LinkUrl method and set the MemberValue to the string returned from the function ( URL) by overriding the Resolve method.
CONFIG
<sitecore>
<pipelines>
<getScribanItemMembers>
<processor type="Foundation.Extension.Pipelines.GetLinkUrl,Foundation.Extension" resolve="true" />
</getScribanItemMembers>
</pipelines>
</sitecore>
We are placing our class in the getScribanItemMembers pipeline - don't forget to add the resolve element if you want the context to get resolved in your constructor.
All other existing pipelines related to scribans are stored in the Sitecore.XA.Foundation.Scriban.config file and to write your custom code you need to add Sitecore.XA.Foundation.Scriban.dll also .
IMPLEMENTATION
Once you are done with the code, deploy the necessary items to the webroot. In the rendering variants, you can use your custom Item Member by using this syntax {{i_item.linkurl}}.
And the URL will be seen below.
Sitecore 9.3 provides various new things with the introduction of each version, but the best part of it is to extend those features. Today we are going to extend the scriban`s functionality by adding our custom scriban and we will add a new Member for the scriban .
CODE
public class GetLinkUrl : GetScribanItemMember
{
private readonly IContext context;
protected override string MemberName => "linkurl";
public GetLinkUrl(IContext context)
{
this.context = context;
}
protected override void Resolve(GetScribanItemMembersPipelineArgs args)
{
if(args.Mode == MemberMode.Name)
{
args.Members.Add(MemberName);
}
else
{
var field = args.Item.Fields["{EE230482-43D4-4144-9EA4-CD33494AC364}"];
var fieldType = FieldTypeManager.GetField(field);
var linkfieldUrl = "";
if(fieldType is LinkField)
{
linkfieldUrl = LinkUrl(field);
}
args.MemberValue = (object)linkfieldUrl;
}
}
public static string LinkUrl(LinkField linkField)
{
if (linkField != null)
{
switch (linkField.LinkType.ToLower())
{
case "internal":
// Use LinkMananger for internal links, if link is not empty
return linkField.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(linkField.TargetItem) : string.Empty;
case "media":
// Use MediaManager for media links, if link is not empty
return linkField.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(linkField.TargetItem) : string.Empty;
case "external":
// Just return external links
return linkField.Url;
case "anchor":
// Prefix anchor link with # if link if not empty
return !string.IsNullOrEmpty(linkField.Anchor) ? "#" + linkField.Anchor : string.Empty;
case "mailto":
// Just return mailto link
return linkField.Url;
case "javascript":
// Just return javascript
return linkField.Url;
default:
// Just please the compiler, this
// condition will never be met
return linkField.Url;
}
}
return "";
}
}
The GetLinkUrl class is inheriting from the GetScribanItemMember class and then we added a new Member in the scriban using the MemberName property and we are then checking if the field type is a link field then get the URL from the LinkUrl method and set the MemberValue to the string returned from the function ( URL) by overriding the Resolve method.
CONFIG
<sitecore>
<pipelines>
<getScribanItemMembers>
<processor type="Foundation.Extension.Pipelines.GetLinkUrl,Foundation.Extension" resolve="true" />
</getScribanItemMembers>
</pipelines>
</sitecore>
We are placing our class in the getScribanItemMembers pipeline - don't forget to add the resolve element if you want the context to get resolved in your constructor.
All other existing pipelines related to scribans are stored in the Sitecore.XA.Foundation.Scriban.config file and to write your custom code you need to add Sitecore.XA.Foundation.Scriban.dll also .
IMPLEMENTATION
Once you are done with the code, deploy the necessary items to the webroot. In the rendering variants, you can use your custom Item Member by using this syntax {{i_item.linkurl}}.
This was just an example of how you can extend sxa features. Happy Coding !!.
Comments
Post a Comment