Meny NO

Controlling allowed types in a ContentArea using an interface

Recently I've been involved in a project that features a great deal of different content types. Early on I figured that using an AllowedTypesAttribute on ContentAreas would be helpful for the editors, since you might not want to drop exactly everything, everywhere.

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()
                                                                        .Where(p => typeof(IContentAreaContent).IsAssignableFrom(p) && !p.IsInterface)

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;
kommentarer drevet av Disqus