7

We need to apply attributes to rendered links conditionally. For example, if an internal link points to a specific template, add an attribute to the rendered anchor tag. Our initial efforts have focused on the <renderField> pipeline. We have patched in a processor attempted to modify the args.RenderParameters, but so far we're out of luck.

Ultimately, this is so that we can open certain links with modal windows.

1
  • Can you explain what's not working with the patched processor approach? This approach was my first thought as well so maybe if we can understanding what's not working we can maybe find a way to fix that.
    – DougCouto
    Commented Nov 29, 2016 at 16:02

1 Answer 1

9

We have just recently implemented a solution for this, to handle links to a specific template (Modal) differently.

We created a custom renderField pipeline processor like the following:

namespace Pipelines
{
    public class RenderModalLink
    {
        public void Process(RenderFieldArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

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

            var field = Sitecore.Data.Fields.FieldTypeManager.GetField(args.GetField());

            // Is it a linkField
            if (field is LinkField)
            {
                // Explicit cast required for some reason
                LinkField linkField = (LinkField) field;

                // Check if link is
                if (linkField.LinkType.ToLower() == "internal")
                {
                    // Check if field has a value
                    if (linkField.TargetItem == null)
                        return;

                    // Check if target item derives modal template (Synthesis)
                    var modal = linkField.TargetItem.As<IModalItem>();

                    if (modal != null)
                    {

                        // Add your custom parameters (HTML attributes) here
                        args.Parameters.Add("data-modal", linkField.GetFriendlyUrl());
                        args.Parameters.Add("href", "javascript:void(0);");
                    }
                }
            }
        }
    }
}

and patched it in after the SetParameters processor:

<renderField>
  <processor type="Pipelines.RenderModalLink" patch:after="processor[@type='Sitecore.Pipelines.RenderField.SetParameters, Sitecore.Kernel']"/>
</renderField>
1
  • Great, we were extremely close it appears. We were not using the .Add method which appears to have caused issues.
    – jrap
    Commented Nov 29, 2016 at 16:26

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.