This tutorial will show you how to use a custom pipeline processor to process fields in a Sitecore environment. In particular, we will deal with LinkField types to make sure that, for further security, external links have the rel="noopener noreferrer" tag. First, we'll look at the ExternalLinks class's C# code, which adds the required characteristics and processes the LinkField. The pipeline configuration that adds our custom processor into the Sitecore renderField pipeline will then be examined in the.config file.

You will know how to design and set up a custom pipeline processor in Sitecore to efficiently manage external links at the end of this lesson. Let's get started!

Pipeline C# Code
In this code we process a field in a Sitecore context, specifically handling LinkField types. It ensures that external links have the rel="noopener noreferrer" attribute added for security purposes.
public class ExternalLinks
{
    public void Process(RenderFieldArgs args)
    {
        Assert.ArgumentNotNull(args, "args");

        if (Sitecore.Context.Site.DisplayMode == Sitecore.Sites.DisplayMode.Edit)
            return;

        var field = FieldTypeManager.GetField(args.GetField());

        if (field is LinkField linkField)
        {
            if (linkField.IsInternal || string.IsNullOrEmpty(linkField.Url))
                return;

            args.Parameters.Add("rel", "noopener noreferrer");
        }
    }
}

Pipeline .config
And now we have here the configuration of the pipeline. This configuration sets up the ExternalLinks class to be executed as part of the renderField pipeline in Sitecore. It ensures that the ExternalLinks processor runs after the GetLinkFieldValue processor. Additionally, it includes a setting to control the protection of external links with a target="_blank" attribute.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <settings>
            <setting name="ProtectExternalLinksWithBlankTarget" value="false" />
        </settings>
        <pipelines>
            <renderField>
                <processor
                    type="Foundation.Infrastructure.Pipelines.ExternalLinks, Foundation.Infrastructure"
                    patch:after="processor[@type='Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel']" />
            </renderField>
        </pipelines>
    </sitecore>
</configuration>

Thanks for reading!

And that's it! We created a custom pipeline processor in Sitecore to handle LinkField types. The ExternalLinks class ensures external links include the rel="noopener noreferrer" attribute for security. We integrated this processor into the renderField pipeline, ensuring it runs after the GetLinkFieldValue processor. This setup helps maintain the security and integrity of external links in your Sitecore application.

If you have any questions or ideas in mind, it'll be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.

HostForLIFE ASP.NET Core 9.0 Hosting

European Best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.