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.
Let's start on taking a look on how you normally add an editor for a given combination of type and ui hint:
[EditorDescriptorRegistration(TargetType = typeof(ContentReference), UIHint = FotoWareUiHint.FotoWareImage)]
[EditorDescriptorRegistration(TargetType = typeof(ContentReference), UIHint = FotoWareUiHint.DisableDefaultEpiserverMedia)]
public class FotoWareImageContentReferenceEditorDescriptor : ImageReferenceEditorDescriptor
{
public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
{
ClientEditingClass = "fotowareintegration/editors/FotoWareThumbnailSelector";
var settings = ServiceLocator.Current.GetInstance<IFotoWareSettings>();
//Additional code to set up the editor...
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:
[EPiServer.Framework.InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class FotowareInitializableModule : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
IFotoWareCommerceSettings fotowareCommerceSettings;
ServiceLocator.Current.TryGetExistingInstance<IFotoWareCommerceSettings>(out fotowareCommerceSettings);
if (fotowareCommerceSettings != null && fotowareCommerceSettings.PreventCommerceEditorIntegration)
{
return;
}
var metadataHandlerRegistry = ServiceLocator.Current.GetInstance<MetadataHandlerRegistry>();
metadataHandlerRegistry.RegisterMetadataHandler(typeof(string), new FotoWareImageUrlEditorDescriptor(), "commercemedia", EditorDescriptorBehavior.OverrideDefault);
}
So, what can we learn from the code above?
- 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: