Controlling allowed types in a ContentArea using an interface

13.02.2015 13.00.00

Given the way that the AllowedTypesAttribute works, you have to specify each type that you might want inside that ContentArea, no inheritance or interfacing allowed. I came up with a fairly simple workaround for situations where you want to control the allowance on all ContentAreas that have no AllowedTypesAttribute defined in the solution. Mainly to avoid having to enter a lot of text.

Step 1: Create an interface

Create a new interface, preferably somewhere where interfaces like to grow.

public interface IContentAreaContent : IContentData { }

Step 2: Create a configuration class

In the same project as your models. Or modify the below example accordingly.

public static class ModelConfiguration
{
    public static readonly Type[] DefaultAllowedContentTypes = Assembly.GetExecutingAssembly()
                                                                        .GetTypes()
                                                                        .Where(p => typeof(IContentAreaContent).IsAssignableFrom(p) && !p.IsInterface)
                                                                        .ToArray();
}

Step 3: Create an EditorDescriptor

This step overrides the ContentAreaEditorDescriptor and specifies all the classes that implement IContentAreaContent for you.

[EditorDescriptorRegistration(TargetType = typeof(ContentArea), EditorDescriptorBehavior = EditorDescriptorBehavior.OverrideDefault)]
public class AllowedTypeContentAreaEditorDescriptor : ContentAreaEditorDescriptor
{
    public AllowedTypeContentAreaEditorDescriptor()
    {
        AllowedTypes = ModelConfiguration.DefaultAllowedContentTypes;
    }
}