Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pipeline processor to add help text to a field in Sitecore.
using System;
using System.Linq;
using System.Web;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Mvc.Presentation;
using Sitecore.Pipelines.GetChromeData;
namespace DEG.SC.ExperienceEditor
{
public class AddFieldHelpText : GetChromeDataProcessor
{
public override void Process(GetChromeDataArgs args)
{
Assert.ArgumentNotNull(args, "args");
Assert.IsNotNull(args.ChromeData, "Chrome Data");
if (!"field".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
return;
var renderingContext = RenderingContext.CurrentOrNull;
if(renderingContext == null)
return;
var currentRendering = GetCurrentRendering();
if(currentRendering == null)
return;
var parameterKey = args.ChromeData.DisplayName + "_HelpText";
var parameterValue = GetParameterValue(currentRendering, parameterKey);
if(string.IsNullOrWhiteSpace(parameterValue))
return;
args.ChromeData.DisplayName += " " + Translate.Text(parameterValue);
}
private string GetParameterValue(Rendering currentRendering, string parameterKey)
{
if (currentRendering.Parameters.Any(x => x.Key.Equals(parameterKey, StringComparison.OrdinalIgnoreCase)))
return currentRendering.Parameters.Where(x => x.Key.Equals(parameterKey, StringComparison.InvariantCultureIgnoreCase))
.Select(x => x.Value)
.First();
var renderingItemParameters = HttpUtility.ParseQueryString(currentRendering.RenderingItem.Parameters);
return renderingItemParameters[parameterKey] ?? "";
}
private Rendering GetCurrentRendering()
{
var contextService = Sitecore.Mvc.Common.ContextService.Get();
if (contextService == null)
return null;
var renderingContext = contextService.GetInstances<RenderingContext>().LastOrDefault();
return renderingContext == null
? null
: renderingContext.Rendering;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.