0

I'm using sitecore 9.1 and SXA 1.8. I am trying to read a field of type "General Link" in a rendering variant. I've already tried before to read it using NVelocity and it worked but is there a ootb way to directly read its value?

2
  • What do you mean by "its value"? Do you mean the url, the link description, the title text etc.. ? A General link contains many values. What are you trying to render out?
    – Richard Seal
    Jan 7, 2019 at 13:59
  • yes, I mean the url whether internal link or external link. Jan 7, 2019 at 14:21

3 Answers 3

1

A simple "Field" variant field will do the trick. It will display internal and external links (the title and the href) just like the Sitecore renderer would.

If you need to get the url alone (not rendered in a <a> tag), I think there is no ootb solution.

3

There is no way to get this OOTB, you will need to create a template renderer for NVelocity.

You will need a class with a static method:

public class FieldTokens
{
    public static string GetGeneralLinkUrl(Item item, string fieldName)
    {
        if (string.IsNullOrEmpty(fieldName))
        {
            return string.Empty;
        }

        if (item == null)
        {
            return string.Empty;
        }

        var field = item.Fields[fieldName];
        var url = string.Empty;
        if (field != null)
        {
            switch (field.Type)
            {
                case "General Link":
                    url = ItemExtensions.LinkUrl(field);
                    break;
            }
        }

        return url;
    }
}

That is a very basic method that will get the url from a general link or return an empty string if the field is not a general link or if the field does not exist on the item.

You need to create a pipeline process that will add that FieldTokens class to the NVelocity renderers:

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

And finally patch that in via config:

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

Once you have all this in place, you can call it from an NVelocity template like this:

Link Text

Caveats

The above code worked on 9.0.2 with SXA 1.7.1, It should work on 9.1/1.8 but its not tested there.

The next note would be, why do you need the url? If you are just rendering the link, its better to use the field renderer as this will render the link, link text, link title, target etc... it will also mean the url is editable in the experience editor. This is normally better than building the link markup manually.

The only time I would use this would be if I needed an image or some complex markup inside the link.

2

If you will be using it in attribute, you can just enclose the Field Name in $(FieldName). Please see sample below: enter image description here

In this case, it renders like this: <a href="/about-us"></a> *I used the Section option and set the tag to anchor

Section Item

Attribute and the value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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