Custom Header Icon for your Items?
What am I changing?
How do you do it?
<pipelines> <renderContentEditorHeader> <processor type="MyProject.Feature.Image.Processors.GetEditorIcon, MyProject.Feature.Image" patch:before="processor[@type='Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditorHeader.AddTitle, Sitecore.Client']" resolve="true" /> </renderContentEditorHeader> </pipelines>And here's the custom processor:
namespace MyProject.Feature.Image.Processors { public class GetEditorIcon { private readonly ISitecoreService _sitecoreService; public GetEditorIcon(ISitecoreServiceFactory sitecoreServiceFactory) { _sitecoreService = sitecoreServiceFactory.CreateInstance(); } public void Process(RenderContentEditorHeaderArgs args) { Assert.ArgumentNotNull(args, "args"); var item = args.Item; using (var htmlTextWriter = new HtmlTextWriter(new StringWriter())) { var imageBuilder = new ImageBuilder(); if (item.TemplateID == Constants.MemberDetailsPage) { var provider = _sitecoreService.GetItem<FacultyStaffMember>(item.ID.Guid); if (!string.IsNullOrEmpty(provider?.FacultyMemberProfileImage?.Src)) { var src = provider.FacultyMemberProfileImage.Src; var urlString = new UrlString(src + "?height=32&width=32") { ["rev"] = item[FieldIDs.Revision], ["la"] = item.Language.ToString() }; imageBuilder.Src = urlString.ToString(); imageBuilder.Class = "scEditorHeaderIcon"; htmlTextWriter.Write("<span class=\"scEditorHeaderIcon\">"); htmlTextWriter.Write(imageBuilder.ToString()); htmlTextWriter.Write("</span>"); //Remove other icon added args.Parent.Controls.Clear(); args.EditorFormatter.AddLiteralControl(args.Parent, htmlTextWriter.InnerWriter.ToString()); } } } } } }The process method does the following:
- Checks the template of the item to ensure that it is using the template we are wanting to update.
- We then get the item (in this case using GlassMapper), but you could just use the Sitecore item
- We then build the image HTML and replace the Sitecore one in the header
This processor gets called every time the item is refreshed in the Content Editor.