Tags: Properties

Move property to a non-existing tab!

I’ve previously described how you can move built-in Episerver properties to a different tab. This will only work perfectly if the destination tab already contains another property for the same page. If not, the title will look strange, the sort order will be missing, etc. Today we will move a property to a non-existing tab!

Using an EditorDescriptor and GroupSettings we may create, and modify, tabs dynamically. You may name the tabs differently for different editors, you may hide tabs for some user groups, you may add the number of properties to the tab name, or you may create entirely new tabs.

In this example, I move the shortcut property from the Settings tab to a new tab only visible to editors with admin access to the content.

[EditorDescriptorRegistration(TargetType = typeof(ContentData))]
public class MoveShortcutDescriptor : EditorDescriptor
{
   public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable attributes)
   {
      foreach (var modelMetadata in metadata.Properties)
      {
         var property = (ExtendedMetadata)modelMetadata;
         if (property.PropertyName == "iversionable_shortcut")
         {
            property.GroupName = Global.GroupNames.AdminOnly;
            property.GroupSettings = CreateGroupSettingsIfConfigTabIsMissing(metadata);
         }
      }
   }

   private static GroupSettings CreateGroupSettingsIfConfigTabIsMissing(ExtendedMetadata metadata)
   {
      // If tab already exist, do not attemp to create it.
      if (metadata.Properties.Cast().Any(property => property.GroupName == Global.GroupNames.Config && property.GroupSettings != null))
      {
         return null;
      }

      var ownerContent = metadata.Model as PageData;
      var isAdmin = ownerContent.QueryDistinctAccess(AccessLevel.Administer);

      // Create new tab, visible only to editors with admin access.
      return new GroupSettings(Global.GroupNames.AdminOnly, isAdmin, "epi/shell/layout/SimpleContainer",
         new Dictionary())
      {
         Title = Global.GroupNames.AdminOnly,
         DisplayOrder = 1000
      };
   }
}