codeflood logo

Show some standard fields always

I had a question posed to me today from Krystle Magadia from New Zealand about how to show some of the standard fields without having to switch on standard fields under the view tab in the content editor. The scenario given was that the archive and reminder fields (under the tasks field section) should always be displayed. Normally we interact with these fields using the commands and wizards on the content editors ribbon. Although I can see benefit of not having to click another button to quickly view the archive date for an item.

So how would I make a particular standard field display always? Checking in web.config we find that the inclusion of fields for display in the content editor is managed from a pipeline. This makes it very easy to hack (read: modify, update, alter). The pipeline name is “getContentEditorFields” and by default it contains a single processor; Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.GetFields.

<getContentEditorFields>
  <processor type="Sitecore.Shell.Applications.ContentEditor.
    Pipelines.GetContentEditorFields.GetFields, Sitecore.Client"/>
</getContentEditorFields>

Looking at the GetFields class, there is a virtual method called “CanShowField”. Being that this method is virtual, the developer intends for implementers to override this method when subclassing.

So I could just create a class which inherits from GetFields and just override the CanShowField method. This is too easy!

So for the example above where I always want to see the archive date field my custom class might look something like the following:

using Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields;

namespace ContentEditorHacks
{
  public class CustomGetFields : GetFields
  {
    protected override bool CanShowField(Sitecore.Data.Fields.Field field,
      Sitecore.Data.Templates.TemplateField templateField)
    {
      if (field.ID == Sitecore.FieldIDs.ArchiveDate)
        return true;
      else
        return base.CanShowField(field, templateField);    }
  }
}

So all I’m doing is checking the ID of the field being passed to me against the known field IDs in Sitecore. Returning true out of this method will add that field into the list of displayed fields.

The last step is just to update the pipeline to use my custom processor instead of the default one.

<getContentEditorFields>
  <processor type="ContentEditorHacks.CustomGetFields, ContentEditorHacks"/>
</getContentEditorFields>

And there we go. Open any item in the content editor and you’ll now see your standard field displayed even when not viewing with standard fields turned on in the view tab.

For a bit more info into the UI pipelines and the content editor, go check out Alexey Rusakov’s blog. He posted a few great pieces recently: The renderField Pipeline and Sitecore 6 Pipelines: renderItemTile. The latter describes how to add little indicator icons to existing item icons in the content editor.

Comments

Comments are closed