Created
September 11, 2015 19:56
-
-
Save JRondeau16/b20494a2154064653709 to your computer and use it in GitHub Desktop.
Pipeline processor to add help text to a field in Sitecore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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