The other day I was going through search terms people had used to get to my blog, and discovered a few people made their way to my blog by searching for ‘sitecore hide sections in data template’.
I had built something like this in the past, but no longer remember how I had implemented that particular solution — not that I could show you that solution since it’s owned by a previous employer — and decided I would build another solution to accomplish this.
Before I move forward, I would like to point out that Sitecore MVP Andy Uzick wrote a blog post recently showing how to hide fields and sections in the content editor, though I did not have much luck with hiding sections in the way that he had done it — sections with no fields were still displaying for me in the content editor — and I decided to build a different solution altogether to make this work.
I thought it would be a good idea to let users turn this functionality on and off via a checkbox in the ribbon, and used some code from a previous post — in this post I had build an object to keep track of the state of a checkbox in the ribbon — as a model. In the spirit of that object, I defined the following interface:
namespace Sitecore.Sandbox.Utilities.ClientSettings
{
public interface IRegistrySettingToggle
{
bool IsOn();
void TurnOn();
void TurnOff();
}
}
I then created the following abstract class which implements the interface above, and stores the state of the setting defined by the given key — the key of the setting and the “on” value are passed to it via a subclass — in the Sitecore registry.
using Sitecore.Diagnostics;
using Sitecore.Web.UI.HtmlControls;
namespace Sitecore.Sandbox.Utilities.ClientSettings
{
public abstract class RegistrySettingToggle : IRegistrySettingToggle
{
private string RegistrySettingKey { get; set; }
private string RegistrySettingOnValue { get; set; }
protected RegistrySettingToggle(string registrySettingKey, string registrySettingOnValue)
{
SetRegistrySettingKey(registrySettingKey);
SetRegistrySettingOnValue(registrySettingOnValue);
}
private void SetRegistrySettingKey(string registrySettingKey)
{
Assert.ArgumentNotNullOrEmpty(registrySettingKey, "registrySettingKey");
RegistrySettingKey = registrySettingKey;
}
private void SetRegistrySettingOnValue(string registrySettingOnValue)
{
Assert.ArgumentNotNullOrEmpty(registrySettingOnValue, "registrySettingOnValue");
RegistrySettingOnValue = registrySettingOnValue;
}
public bool IsOn()
{
return Registry.GetString(RegistrySettingKey) == RegistrySettingOnValue;
}
public void TurnOn()
{
Registry.SetString(RegistrySettingKey, RegistrySettingOnValue);
}
public void TurnOff()
{
Registry.SetString(RegistrySettingKey, string.Empty);
}
}
}
I then built the following class to toggle the display settings for our displayable fields:
using System;
namespace Sitecore.Sandbox.Utilities.ClientSettings
{
public class ShowDisplayableFieldsOnly : RegistrySettingToggle
{
private const string RegistrySettingKey = "/Current_User/Content Editor/Show Displayable Fields Only";
private const string RegistrySettingOnValue = "on";
private static volatile IRegistrySettingToggle current;
private static object lockObject = new Object();
public static IRegistrySettingToggle Current
{
get
{
if (current == null)
{
lock (lockObject)
{
if (current == null)
{
current = new ShowDisplayableFieldsOnly();
}
}
}
return current;
}
}
private ShowDisplayableFieldsOnly()
: base(RegistrySettingKey, RegistrySettingOnValue)
{
}
}
}
It passes its Sitecore registry key and “on” state value to the RegistrySettingToggle base class, and employs the Singleton pattern — I saw no reason for there to be multiple instances of this object floating around.
In order to use a checkbox in the ribbon, we have to create a new command for it:
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Sandbox.Utilities.ClientSettings;
namespace Sitecore.Sandbox.Commands
{
public class ToggleDisplayableFieldsVisibility : Command
{
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
ToggleDisplayableFields();
Refresh(context);
}
private static void ToggleDisplayableFields()
{
IRegistrySettingToggle showDisplayableFieldsOnly = ShowDisplayableFieldsOnly.Current;
if (!showDisplayableFieldsOnly.IsOn())
{
showDisplayableFieldsOnly.TurnOn();
}
else
{
showDisplayableFieldsOnly.TurnOff();
}
}
public override CommandState QueryState(CommandContext context)
{
if (!ShowDisplayableFieldsOnly.Current.IsOn())
{
return CommandState.Enabled;
}
return CommandState.Down;
}
private static void Refresh(CommandContext context)
{
Refresh(GetItem(context));
}
private static void Refresh(Item item)
{
Assert.ArgumentNotNull(item, "item");
Context.ClientPage.ClientResponse.Timer(string.Format("item:load(id={0})", item.ID), 1);
}
private static Item GetItem(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
return context.Items.FirstOrDefault();
}
}
}
The command above leverages the instance of the ShowDisplayableFieldsOnly class defined above to turn the displayable fields feature on and off.
I followed the creation of the command above with the definition of the ribbon checkbox in the core database:
![show-displayable-fields-checkbox-defined]()
The command name above — which is set in the Click field — is defined in the patch configuration file towards the end of this post.
I then created the following data template with a TreelistEx field to store the displayable fields:
![displayable-fields-template]()
The TreelistEx field above will pull in all sections and their fields into the TreelistEx dialog, but only allow the selection of template fields, as is dictated by the following parameters that I have mapped in its Source field:
DataSource=/sitecore/templates/Sample/Sample Item&IncludeTemplatesForSelection=Template field&IncludeTemplatesForDisplay=Template section,Template field&AllowMultipleSelection=no
I then set this as a base template in my sandbox solution’s Sample Item template:
![set-displayable-fields-template-as-base]()
In order to remove fields, we need a <getContentEditorFields> pipeline processor. I built the following class for to serve as one:
using System;
using Sitecore.Configuration;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Data.Templates;
using Sitecore.Diagnostics;
using Sitecore.Shell.Applications.ContentManager;
using Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields;
using Sitecore.Sandbox.Utilities.ClientSettings;
namespace Sitecore.Sandbox.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields
{
public class RemoveUndisplayableFields
{
public void Process(GetContentEditorFieldsArgs args)
{
Assert.ArgumentNotNull(args, "args");
Assert.ArgumentNotNull(args.Item, "args.Item");
Assert.ArgumentCondition(!string.IsNullOrWhiteSpace(DisplayableFieldsFieldName), "DisplayableFieldsFieldName", "DisplayableFieldsFieldName must be set in the configuration file!");
if (!ShowDisplayableFieldsOnly.Current.IsOn())
{
return;
}
foreach (Editor.Section section in args.Sections)
{
AddDisplayableFields(args.Item[DisplayableFieldsFieldName], section);
}
}
private void AddDisplayableFields(string displayableFieldIds, Editor.Section section)
{
Editor.Fields displayableFields = new Editor.Fields();
foreach (Editor.Field field in section.Fields)
{
if (IsDisplayableField(displayableFieldIds, field))
{
displayableFields.Add(field);
}
}
section.Fields.Clear();
section.Fields.AddRange(displayableFields);
}
private bool IsDisplayableField(string displayableFieldIds, Editor.Field field)
{
if (IsStandardValues(field.ItemField.Item))
{
return true;
}
if (IsDisplayableFieldsField(field.ItemField))
{
return false;
}
return IsStandardTemplateField(field.ItemField)
|| string.IsNullOrWhiteSpace(displayableFieldIds)
|| displayableFieldIds.Contains(field.ItemField.ID.ToString());
}
private bool IsDisplayableFieldsField(Field field)
{
return string.Equals(field.Name, DisplayableFieldsFieldName, StringComparison.CurrentCultureIgnoreCase);
}
private static bool IsStandardValues(Item item)
{
if (item.Template.StandardValues != null)
{
return item.Template.StandardValues.ID == item.ID;
}
return false;
}
private bool IsStandardTemplateField(Field field)
{
Assert.IsNotNull(StandardTemplate, "The Stardard Template could not be found.");
return StandardTemplate.ContainsField(field.ID);
}
private static Template GetStandardTemplate()
{
return TemplateManager.GetTemplate(Settings.DefaultBaseTemplate, Context.ContentDatabase);
}
private Template _StandardTemplate;
private Template StandardTemplate
{
get
{
if (_StandardTemplate == null)
{
_StandardTemplate = GetStandardTemplate();
}
return _StandardTemplate;
}
}
private string DisplayableFieldsFieldName { get; set; }
}
}
The class above iterates over all fields for the supplied item, and adds only those that were selected in the Displayable Fields TreelistEx field, and also Standard Fields — we don’t want to remove these since they are shown/hidden by the Standard Fields feature in Sitecore — to a new Editor.Fields collection. This new collection is then set on the GetContentEditorFieldsArgs instance.
Plus, we don’t want to show the Displayable Fields TreelistEx field when the feature is turned on, and we are on an item. This field should only display when we are on the standard values item when the feature is turned on — this is how we will choose our displayable fields.
Now we have to handle sections without fields — especially after ripping them out via the pipeline processor above.
I built the following class to serve as a <renderContentEditor> pipeline processor to do this:
using System.Linq;
using Sitecore.Diagnostics;
using Sitecore.Shell.Applications.ContentManager;
using Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor;
namespace Sitecore.Sandbox.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor
{
public class FilterSectionsWithFields
{
public void Process(RenderContentEditorArgs args)
{
Assert.ArgumentNotNull(args, "args");
args.Sections = GetSectionsWithFields(args.Sections);
}
private static Editor.Sections GetSectionsWithFields(Editor.Sections sections)
{
Assert.ArgumentNotNull(sections, "sections");
Editor.Sections sectionsWithFields = new Editor.Sections();
foreach (Editor.Section section in sections)
{
AddIfContainsFields(sectionsWithFields, section);
}
return sectionsWithFields;
}
private static void AddIfContainsFields(Editor.Sections sections, Editor.Section section)
{
Assert.ArgumentNotNull(sections, "sections");
Assert.ArgumentNotNull(section, "section");
if (!ContainsFields(section))
{
return;
}
sections.Add(section);
}
private static bool ContainsFields(Editor.Section section)
{
Assert.ArgumentNotNull(section, "section");
return section.Fields != null && section.Fields.Any();
}
}
}
It basically builds a new collection of sections that contain at least one field, and sets it on the RenderContentEditorArgs instance being passed through the <renderContentEditor> pipeline.
In order for this to work, we must make this pipeline processor run before all other processors.
I tied all of the above together with the following patch configuration file:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<commands>
<command name="contenteditor:ToggleDisplayableFieldsVisibility" type="Sitecore.Sandbox.Commands.ToggleDisplayableFieldsVisibility, Sitecore.Sandbox"/>
</commands>
<pipelines>
<getContentEditorFields>
<processor type="Sitecore.Sandbox.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.RemoveUndisplayableFields, Sitecore.Sandbox">
<DisplayableFieldsFieldName>Displayable Fields</DisplayableFieldsFieldName>
</processor>
</getContentEditorFields>
<renderContentEditor>
<processor patch:before="processor[@type='Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor.RenderSkinedContentEditor, Sitecore.Client']"
type="Sitecore.Sandbox.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor.FilterSectionsWithFields, Sitecore.Sandbox"/>
</renderContentEditor>
</pipelines>
</sitecore>
</configuration>
Let’s try this out.
I navigated to the standard values item for my Sample Item data template, and selected the fields I want to display in the content editor:
![selected-some-fields]()
I then went to an item that uses this data template, and turned on the displayable fields feature:
![displayable-fields-on]()
As you can see, only the fields we had chosen display — along with standard fields since the Standard Fields checkbox is checked.
I then turned off the displayable fields feature:
![displayable-fields-off]()
Now all fields for the item display.
I then turned the displayable fields feature back on, and turned off Standard Fields:
![standard-fields-off-displayable-fields-on]()
Now only our selected fields display.
If you have any thoughts on this, or ideas around making this better, please share in a comment.