1

Linking a document in rich RichText editor like this:

<a href="/-/media/045D86A8E4BA4D5F9DFE3F703D6E75E9.ashx?">2017-02-14-presentation-.pdf</a>

produces the following when rendered: notice the hash query parameter

<a href="/-/media/Documents/presentation.pdf?la=en&;hash=186EBA9A3F2735D00373A9D893F06579"> presentation.pdf</a>

but the issue I have is that the hash= query string stops this from being processed by the overridden ridden handler below... thanks to Kamruz Jaman for the help in getting me started with this code

https://gist.github.com/jammykam/c89e2f4eb62638226e1d005d22802f0a

only when I remove the hash=will the handler be called.

what I want is to remove the hash for certain mimeType (configurable)...

<a href="/-/media/Documents/presentation.pdf?la=en">presentation.pdf</a>

or to be able to process the link even if the hash is there

1 Answer 1

2

I'm always scared when it comes to messing with Sitecore mechanism. But the code below should do the trick.

It replaces the original ProtectedImageLinkRenderer processor which protects all the media links generated from field renderers (including rich text field).

The _ignoredExtensions field can be configured via xml config like in the example and contains a list of extensions that should be ignored.

GetProtectedUrl method checks first if the extension in the generated url is on the list of ignored extensions and if it is, returns unprotected url.

Of course it only works if you have Media.RequestExtension setting set to empty value and links are generated with proper extensions - otherwise all the links are generated with ashx extension by default and it would not be possible to deduce the extension.

using System;
using System.Collections.Generic;
using System.Xml;
using Sitecore.Pipelines.RenderField;
using Sitecore.Xml;

namespace SitecorePlayground.Pipelines.renderField
{
    public class CustomProtectedImageLinkRenderer : ProtectedImageLinkRenderer
    {
        private readonly List<string> _ignoredExtensions = new List<string>();

        protected override string GetProtectedUrl(string url)
        {
            var extension = url.Substring(Math.Max(0, url.LastIndexOf('.'))).TrimStart('.');

            int questionMarkIndex;
            if ((questionMarkIndex = extension.IndexOf('?')) > -1)
            {
                extension = extension.Substring(0, questionMarkIndex);
            }

            if (_ignoredExtensions.Contains(extension.ToLower()))
                return url;

            return base.GetProtectedUrl(url);
        }

        public virtual void AddIgnoredExtension(XmlNode configNode)
        {
            string extension = XmlUtil.GetValue(configNode);
            if (!string.IsNullOrWhiteSpace(extension))
            {
                _ignoredExtensions.Add(extension.ToLower());
            }
        }
    }
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <renderField>
        <processor
          patch:instead="processor[@type='Sitecore.Pipelines.RenderField.ProtectedImageLinkRenderer, Sitecore.Kernel']"
          type="SitecorePlayground.Pipelines.renderField.CustomProtectedImageLinkRenderer, SitecorePlayground">
          <IgnoredExtensions hint="raw:AddIgnoredExtension">
            <wordDocument>docx</wordDocument>
            <pdfDocument>pdf</pdfDocument>
          </IgnoredExtensions>
        </processor>
      </renderField>
    </pipelines>
  </sitecore>
</configuration>

Your Answer

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

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