How do I hide Categories on blocks in Optimizely CMS 12?

  • Page Owner: Not Set
  • Last Reviewed: 2022-04-26

I need to hide categories on blocks, but the code is different than CMS 11, how do I do it?


Answer

Example from GCB.

    [EditorDescriptorRegistration(TargetType = typeof(CategoryList))]
    public class HideBlockCategories : EditorDescriptor
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {

            if (metadata.Parent?.Model is BaseBlock)
            {
                if (metadata.PropertyName == "icategorizable_category")
                {
                    metadata.ShowForEdit = false;
                    metadata.ShowForDisplay = false;
                }
            }
        }
    }

Additional Posts

I found a bug with the above when a CategoryList is used in a property list. In that case, metadata.Parent is null. Simple fix is just to add a null check to the code above:

if (metadata.Parent?.Model is BaseBlock)

Comments

  • I edited John's answer to include the bugfix now.