In the dynamic realm of Sitecore development, optimizing the user experience for content authors and developers is paramount. An unfamous challenge arises when dealing with fields displayed in the content editor.
While it’s a familiar sight for Sitecore users, the need to selectively hide or show fields becomes challenge in diverse scenarios, especially in multi-site or multi-region setups.
In Sitecore, the default presentation of regular fields and standard fields in the content editor is universal. However, like hiding/showing standard fileds, what if your content authors wish to streamline their view by concealing certain fields that are not relevant to their specific market?
For example, consider a scenario where Site A utilizes all fields from a common template, while Site B, sharing the same template, opts to display only a subset of those fields.
The content author for Site B may find it distracting and counterproductive to have unused fields cluttering their item view in the content editor.
The Solution: Conditionally Hiding Fields with “GetContentEditorFields” Pipeline
Sitecore’s pipeline extensibility comes to the rescue with the “GetContentEditorFields” pipeline and its hidden gem, the “GetFields” processor.
Nestled within the “Sitecore.Client.dll,” this processor holds the key to dynamically adjusting field visibility based on your specific requirements.
Implementation: Extending the GetFields Processor
The crux of this solution lies in extending the “GetFields” processor and leveraging the virtual function “CanShowField.” This function acts as a gatekeeper, allowing you to decide, based on custom logic, whether a field should be displayed or remain hidden in the content editor.
public class HideContentEditorFields : Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.GetFields
{
private IList<string> _fieldsToHide;
public virtual string FieldsToBeHidden { get; set; }
protected virtual IList<string> FieldsToBeHiddenList
{
get
{
if (_fieldsToHide != null) return _fieldsToHide;
if (string.IsNullOrWhiteSpace(FieldsToBeHidden))
{
_fieldsToHide = new List<string>();
}
else
{
_fieldsToHide = FieldsToBeHidden.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
}
return _fieldsToHide;
}
}
protected override bool CanShowField(Sitecore.Data.Fields.Field field, TemplateField templateField)
{
Assert.ArgumentNotNull(field, "field");
Assert.ArgumentNotNull(templateField, "templateField");
if (FieldsToBeHiddenList.Contains(field.ID.ToString()) && field.Language == Language.Parse("en-US"))
{
return false;
}
return base.CanShowField(field, templateField);
}
}
Patch the configuration
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<getContentEditorFields>
<processor type="Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.GetFields, Sitecore.Client">
<patch:attribute name="type">YourClassNameWithNameSpace, YourAssemblyName</patch:attribute>
<FieldsToBeHidden>{A3A5E4A9-1B6E-4DD4-9866-BF277A6C55EB}|{2F7EA304-8D61-4C2E-B7D2-907440101F3F}|{31E61F21-D9C2-4450-9CC4-03D9B02F5F03}</FieldsToBeHidden>
</processor>
</getContentEditorFields>
</pipelines>
</sitecore>
</configuration>
By overriding the “CanShowField” method, you gain the ability to inject your logic into the decision-making process. Pass field IDs to be hidden from the configuration to the processor, which then checks the field ID against your conditions, ultimately determining whether the field should be visible.
Conclusion
In the intricate landscape of Sitecore development, the ability to tailor the content editor’s view based on specific site requirements adds a layer of sophistication. By tapping into the “GetFields” processor within the “GetContentEditorFields” pipeline, developers can seamlessly implement conditional field visibility, ensuring a clutter-free and efficient content editing experience.