Hiding and showing edit mode properties on same content type in Optimizely CMS depending on context

At my current client we have the need to hide or show properties for editors in Optimizely edit mode, on the same page type, depending on where/how the page type is used. Our structure consists of global pages, and local instances of the same pages. There are times when global editors should be able to edit properties, while local editors should not. Since we do not want to clutter the interface for the local editors, we simply hide the properties for them.

[AttributeUsage(AttributeTargets.Property)]
public class HidePropertyOnLocalPagesInEditModeAttribute : Attribute, IMetadataAware
{
  public void OnMetadataCreated(ModelMetadata metadata)
  {
    metadata.ShowForDisplay = true;
    metadata.ShowForEdit = true;

    if (metadata is not ExtendedMetadata extendedMetadata)
    {
      return;
    }

    IContent ownerContent = extendedMetadata.FindOwnerContent();
    if (ownerContent is not PageDataBase pageDataBase)
    {
      return;
    }

    if (pageDataBase.GlobalPage != null) // Or some other condition
    {
      metadata.ShowForDisplay = false;
      metadata.ShowForEdit = false;
    }
  }
}

The method we use is having our attribute implement the IMetadataAware interface, and then set the two properties ShowForDisplay and ShowForEdit as needed. In our case, we have a pointer to the GlobalPage version defined as a property on the page types, and if it is set, we know we’re on a local page and can set the metadata accordingly.