9

I need to reach next behavior for sitecore General Link type.

If there is {token} word in a link, in my code it should be replaced with some word. Everewhere. General Links in my project are everywhere, so it would be nice to use something like type constructor or getter just to replace that token.

Can someone advice me how to reach this? Maybe i should use something like pipelines in sitecore.

To explain my needs better, let's imagine i have such link on my site. enter image description here

And this link is in controller rendering.

The question is How to replace this "{{token}}" with something that i need before it is rendered in partial view.

Actually i need to replace all such tokens in every link on my site.

4
  • 2
    I think people need better context, could you give some examples? Dec 22 '17 at 14:36
  • How do tou render the link field? Is it via the FieldRenderer? Dec 22 '17 at 21:55
  • You want to replace {{token}} with what? If it is a string, then you can replace the value in the controller itself when you are getting the value of the general link and after that you render it in the partial view.
    – adarsh
    Dec 24 '17 at 7:27
  • Hishaam Namooya, for now there are too many links in my project, so I can't find each one in controller and handle its rendering. Dec 26 '17 at 12:41
3

Create your token replacer processor:

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

            //Filter to execute your replacer only of specific type of field
            if (args.FieldTypeKey.ToLower() != "general link")
            {
                return;
            }
            args.Result.FirstPart = args.Result.FirstPart.Replace("{{token}}", "something");
            return;
        }
    }
}

Register your processor in renderField pipeline:

<configuration xmlns:x="http://www.sitecore.net/xmlconfig/">
  <sitecore>   
    <pipelines>
      <renderField>        
        <processor x:after="*[@type='Sitecore.Pipelines.RenderField.GetFieldValue, Sitecore.Kernel']" type="YourNamespace.TokenReplacer, YourAssembly" />
      </renderField>
    </pipelines>
  </sitecore>
</configuration>

Now, token should be replaced in all links across the website if you render links using Sitecore API(or GlassMapper or Synthesis).

Here you can read more about it: [1], [2]

1
  • Thank you for your advice, I tried to use it, but it did not work i don't know why. At first, if section was skip only when it was experience editor mode. At second, changing Result.FirstPart didn't change the final version of a link. Maybe i did something wrong, however it didn't work. Dec 26 '17 at 12:39
3

Finally, i solved it by overwriting standard methods of GlassView class.

    public abstract class View<TModel> : GlassView<TModel> where TModel : class
    {
        public new HtmlString Editable<T>(T model, Expression<Func<T, object>> field, object parameters = null)
        {
            return ProcessLinks(GlassHtml.Editable(model, field, parameters));
        }
            .......
            .......
            .......
            .......

        private HtmlString ProcessLinks(string content)
        {
           //here i can replace tokens
           if (!Sitecore.Context.PageMode.IsExperienceEditor)
           {
            content = TokenReplacer.ReplaceCustomTokens(content);
           }
           return content;
        }
     }

At least, that worked.

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.