Conditionally adding or altering editors for Episerver

In Episerver an editor is normally registered for a combination of a .Net type and an optional ui hint using the EditorDescriptorRegistration attribute. In this blog post we’ll look on how you can add editors conditionally at start up.

By
Linus Ekström
9/22/2019
2 min

Let's start on taking a look on how you normally add an editor for a given combination of type and ui hint:

In most cases this is a very convenient way of adding an editor where registration is done by initialization in the Episerver core through the EditorDescriptorRegistration attribute. However, sometimes you might want to make this set up configurable, specifically if you are building a module or base framework where the end user (usually a developer or admin) should be able to configure a certain behaviour. Let's look on how this is done.

For the Fotoware integration, we extend the Episerver Commerce image list with extended possibilities to select items from the Fotoware DAM system. This is configurable through an optional configuration setting (IFotoWareCommerceSettings). The code below shows how this is done:

The first part just checks if the solution has implemented the IFotoWareCommerceSettings interface and set a flag to avoid this configuration – if so we do an early exit from the set up.

After that, we get an instance of the MetadataHandlerRegistry from the ServiceLocator. This is the server-side "engine" of the editing interface in Episerver which keeps track of different editor configuration and generates metadata to the client based on the object and the available editors.

Last, we call RegisterMetadataHandler to register a custom editor for the commerce media list. In this case, there is no specific type that the default editor is connected to, so we've had to use reflection to find the ui hint that the default editor uses: "commercemedia". Notice that we define the last parameter as EditorDescriptorBehavior.OverrideDefault to ensure that we replace the default behaviour. If you want to know more about the different options for this, I suggest that you read my blog post from 2013 about this.

This is how the final result look like when editing items in the Commerce catalog: