Skip to content

Instantly share code, notes, and snippets.

@5up3rman
Created October 12, 2016 13:27
Show Gist options
  • Save 5up3rman/29bf05147f0e4abbaeea39bb6a6165ea to your computer and use it in GitHub Desktop.
Save 5up3rman/29bf05147f0e4abbaeea39bb6a6165ea to your computer and use it in GitHub Desktop.
public class EditorFormatter
{
public RenderContentEditorArgs Arguments { get; set; }
public List<IMappedItem> MappedItems { get; set; } = new List<IMappedItem>();
public EditorFormatter(RenderContentEditorArgs args)
{
Arguments = args;
// We want to make sure the Item is under the Content Node and that the item is not null.
if (args.Item == null || !args.Item.Paths.IsContentItem)return;
// Run the rules and set it to MappedItems
var rules = new ContentEditorRulesProcessor(args.Item);
MappedItems = rules.MappedItems;
}
#region Customizations
public void RenderSection(Editor.Section section, System.Web.UI.Control parent, bool readOnly)
{
// Get the MappedItem that is a section and matches this Sections Name.
var mappedSection =
MappedItems.FirstOrDefault(x => x.Type.Equals(MappedItemType.Section) && x.OriginalName.Equals(section.Name));
// Check to see if this Section is Hidden
if (mappedSection != null && mappedSection.Hide)return;
// Get the Customized Section Name from the MappedSection if it is not null, if null
var sectionName = mappedSection?.NewTitle ?? section.DisplayName;
var sectionCollapsed = section.IsSectionCollapsed;
var renderFields = !sectionCollapsed || UserOptions.ContentEditor.RenderCollapsedSections;
this.RenderSectionBegin(parent, section.ControlID, section.Name, sectionName, section.Icon, sectionCollapsed, renderFields);
if (renderFields)
{
foreach (Editor.Field field in section.Fields)
this.RenderField(parent, field, readOnly);
}
this.RenderSectionEnd(parent, renderFields, sectionCollapsed);
}
public void RenderField(System.Web.UI.Control parent, Editor.Field field, bool readOnly)
{
var itemField = field.ItemField;
var fieldType = this.GetFieldType(itemField);
// Get the Mapped Field
var mappedField =
MappedItems.FirstOrDefault(x => x.Type.Equals(MappedItemType.Field) && x.OriginalName.Equals(field.ItemField.Name));
// Determine if the Field is Hidden by the Rules
if (mappedField != null && mappedField.Hide) return;
// RenderLabel Here
// Code has been removed to shorten the blog post. The code in it's entirety is available for download at the end of this post.
}
public void RenderLabel(System.Web.UI.Control parent, Editor.Field field, Item fieldType, IMappedItem mappedField, bool readOnly)
{
var itemField = field.ItemField;
var language = this.Arguments.Language;
// If the Mappedfield is not null and the ShortDescription is not null return that, else the Item's ToolTip
var toolTip = mappedField?.ShortDescription ?? itemField.ToolTip;
var fieldTitle = field.TemplateField.GetTitle(language);
if (string.IsNullOrEmpty(fieldTitle))
fieldTitle = field.TemplateField.IgnoreDictionaryTranslations
? itemField.Name
: Translate.Text(itemField.Name);
// Set Field Title to Mapped Field's New Title
if (!string.IsNullOrEmpty(mappedField?.NewTitle))
{
var newTitle = field.TemplateField.IgnoreDictionaryTranslations
? mappedField.NewTitle
: Translate.Text(mappedField.NewTitle);
// Display the new Title along with the original Field Title
fieldTitle = $"{newTitle} <b>[{fieldTitle}]</b>";
}
// Code has been removed to shorten the blog post. The code in it's entirety is available for download at the end of this post.
}
#endregion
// ... Additional code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment