Sitecore: Hide fields from Content Editor

Sitecore: Hide fields from Content Editor

Very often every Sicore developer fas faced with the necessity of adding new fields for some particular template. And this task is pretty easy! After new field has been added it appeared in Sitecore Content Editor.

But let’s imagine the situation when there is a need to associate new field with a template and not to show it in Sitecore Content Editor for everyone by some condition or don’t show it for anyone at all.

Good example is a system Sitecore fields that are hidden by default (e.g. information about statistics etc.). If editor has logged-in as an administrator, such hidden fields could be shown by enabling the checkbox “Standard fields” in the “View” Ribbon.

show_standard_fields

To add fields which are only visible when this checkbox is enabled is pretty easy. There is a (not well known) pipeline getContentEditorFieldswhere visibility for the field could be specified. The following processor overrides the default one and shows or hides a field depending on the pipeline processor configuration:

public class ConfigureHiddenFields : Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.GetFields
    {
        private IList<string> _fieldsToHide;

        public virtual string HiddenFields { get; set; }

        protected virtual IList<string> FieldsToHide
        {
            get
            {
                if (_fieldsToHide == null)
                {
                    if (string.IsNullOrWhiteSpace(HiddenFields))
                    {
                        _fieldsToHide = new List<string>();
                    }
                    else
                    {
                        _fieldsToHide = HiddenFields.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    }
                }

                return _fieldsToHide;
            }
        }

        protected override bool CanShowField(Field field, TemplateField templateField)
        {
            Assert.ArgumentNotNull(field, "field");
            Assert.ArgumentNotNull(templateField, "templateField");

            if (!UserOptions.ContentEditor.ShowSystemFields && (FieldsToHide.Contains(field.ID.ToString()) || FieldsToHide.Contains(field.Name)))
            {
                return false;
            }

            return base.CanShowField(field, templateField);
        }
    }

After class has been created there is a need to patch the default processor from Sitecore in the config file. Configuration for all fields which should be hidden by default (pipe separated) listed below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getContentEditorFields>
        <processor type="Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields.ConfigureHiddenFields, Sitecore.Client">
          <patch:attribute name="type">AndreyVinda.ContentEditor.Pipelines.GetContentEditorFields.ConfigureHiddenFields, AndreyVinda.ContentEditor.Pipelines</patch:attribute>
          <HiddenFields>{ED27382D-81BD-4425-B4DC-D44D921ED01C}|{12346B8D-211B-4B4E-A24C-0C930739D341}</HiddenFields>
        </processor>
      </getContentEditorFields>
    </pipelines>
  </sitecore>
</configuration>

In case there is a need to completely hide the field without even a possibility to show it the condition with check !UserOptions.ContentEditor.ShowSystemFields should be removed from the pipeline processor class.

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s