Sitecore 8.2 MVC multi-line field not rendering line breaks correctly

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

In recent versions of Sitecore – at least 8.2 and some versions of 8.1 – line breaks in multi-line fields are not rendered as <br/> tags when output on a page although they are supposed to.
@Html.Sitecore().Field("Multi line field")
When outputting fields like above it runs the renderField pipeline to get the content of the field.
You can force the correct behavior adding the linebreaks parameter.
@Html.Sitecore().Field("Multi line field", new { linebreaks = "<br/>" })
This will now work as expected and render all line breaks as the value of that parameter – <br/>. You can use anything you like here. When the parameter is not there, it is supposed to fall back to using <br/>, but it currently doesn’t.
Sitecore is tracking this bug by reference number 95002.
It’s pretty easy to fix though. One way (as the supplied fix by Sitecore) is to replace the GetMemoFieldValue processor in the renderField pipeline with a fixed version, which falls back to using <br/> if the linebreaks parameter is null.
public class GetMemoFieldValue
{
public void Process(RenderFieldArgs args)
{
switch (args.FieldTypeKey)
{
case "memo":
case "multi-line text":
string linebreaks = args.RenderParameters["line-breaks"]
?? args.RenderParameters["linebreaks"])
?? "<br/>";
args.Result.FirstPart = GetMemoFieldValue.Replace(args.Result.FirstPart, linebreaks);
args.Result.LastPart = GetMemoFieldValue.Replace(args.Result.LastPart, linebreaks);
break;
}
}
private static string Replace(string output, string linebreaks)
{
output = output.Replace("\r\n", linebreaks);
output = output.Replace("\n\r", linebreaks);
output = output.Replace("\n", linebreaks);
output = output.Replace("\r", linebreaks);
return output;
}
}
Then you just replace the existing GetMemoFieldValue processor with the fixed on with a config patch.