2

I am using SXA 1.9 I have a rendering variant that uses a plain old General Link. I can render the Link including description, but I need the link description on a different place in my variant, I did not find something ootb, all I have seen is that I could create a custom token, for example $teaserlinkdescription. Is this possible without a token, am I missing something?

If someone is interested, this is the current code. Only drawback is that this will just work for a single field, if the Teaser has two Link Fields this code is useless:

    public class ResolveTeaserLinkDescription : ResolveVariantTokensProcessor
{
    static readonly ID TeaserLinkId = new ID("FE713FFA-8D9F-6275-813D-06DD7A5EABAA");
    public override string Token
    {
        get
        {
            return "$teaserlinkdescription";
        }
    }

    public override void ResolveToken(ResolveVariantTokensArgs args)
    {
        LinkField linkField = this.GetLinkField(args.ContextItem);
        string resultText = linkField?.Text;

        if (resultText != string.Empty)
        {
            if (args.ResultControl != null)
                args.ResultControl.Controls.Add((Control)new LiteralControl()
                {
                    Text = resultText
                });
            else
                args.Result = resultText;
        }
    }

    private LinkField GetLinkField(Item item)
    {
        return (LinkField)item.Fields[TeaserLinkId];
    }
}
  • I had the same requirement and I didn't find another solution. I've implemented a custom token. – Artsem Prashkovich Sep 24 at 12:13
  • @ArtsemPrashkovich thank you. Is your solution more generic? I only found a way to create this token but it will work just on one specific link field. – Bgl86 Sep 24 at 12:27
  • I've mistaken. I've used NVelocity template instead of custom tokens. Please find my answer below. It is generic enough. – Artsem Prashkovich Sep 24 at 12:40
2

I had the same requirement not so far ago. After googling, I found only one solution and it was to implement custom NVelocity template:

 public class GeneralLinkTool
    {
        public static string GetLinkDescription(Item item, string fieldName = null)
        {
            string value = string.Empty;
            if (item != null)
            {
                if (!string.IsNullOrEmpty(fieldName))
                {
                    var field = item.Fields[fieldName];
                    if (field != null && field.Type == "General Link")
                    {
                        Sitecore.Data.Fields.LinkField link = field;
                        value = link.Text;
                    }
                }
            }
            return value;
        }
    }

Then you need to register the template:

public class AddTemplateRenderers : IGetTemplateRenderersPipelineProcessor
    {
        public void Process(GetTemplateRenderersPipelineArgs args)
        {
            args.Context.Put("fieldTokens", new GeneralLinkTool());
        }
    }

and:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getVelocityTemplateRenderers>
        <processor type="YourNamespace.AddTemplateRenderers, YourNamespace"
                   patch:after="processor[@type='Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.GetVelocityTemplateRenderers.GetVelocityTemplateRenderers, Sitecore.XA.Foundation.Variants.Abstractions']"  />
      </getVelocityTemplateRenderers>
    </pipelines>
  </sitecore>
</configuration>

Now you can use it by adding the Template variant:

template variant

The following line will return you a link description:

$fieldTokens.GetLinkDescription($item, "fieldName")

template variant usage

  • This is awesome, thank you – Bgl86 Sep 24 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.