Custom root for category selection and EditorDescriptorBehavior
In this blog post I’ll describe how to set a custom root for a category property. The code for this is pretty simple:
[EditorDescriptorRegistration(TargetType = typeof(CategoryList), UIHint = "customcategoryroot", EditorDescriptorBehavior = EditorDescriptorBehavior.ExtendBase)]
public class CategoryListEditorDescriptor : EditorDescriptor
{
public override void ModifyMetadata(EPiServer.Shell.ObjectEditing.ExtendedMetadata metadata, System.Collections.Generic.IEnumerable attributes)
{
metadata.EditorConfiguration["root"] = Category.GetRoot().Categories[0].ID;
}
}
The actual logic is pretty simple, set the “root” property to the widget that is responsible for editing (this only works in 7.5 due to some changes in the client widget to support this). In this sample the settings will only affect any CategoryLists tagged with the UIHint “customcategoryroot” but removing this will make the descriptor affect all category-properties, including the built in one.
EditorDescriptorBehavior
So how does the editing system know how to edit the property since we are not defining an editor widget? The answer to this is that we have defined the new attribute property EditorDescriptorBehavior. In this case we are setting this property to EditorDescriptorBehavior.ExtendBase which means that we want to run the default editor descriptors for the type (in this case the default descriptor for CategoryList without any UIHint) first and then this editor descriptor. This makes is possible to extend the behavior without taking having to know about the class responsible for the default implementation. There are a few posibilities to set for this property which the SDK explains like this:
///
/// Adds this descriptor to the list of descriptors for the given type and ui hint combination.
///
Default = 0,
///
/// Adds this descriptor to the list of descriptors for the given type and ui hint combination and
/// makes sure that the descriptors registered without a ui hint for the type are called before this descriptor.
///
/// This is only valid in combination with a ui hint.
ExtendBase = 1,
///
/// Removes any existing descriptors for the type/ui hint combination and then adds this descriptor.
///
OverrideDefault = 2,
///
/// Adds this descriptor last in the list of descriptors for the given type and ui hint combination.
///
/// If several descriptors are defined this way the order of execution is undefined.PlaceLast = 3
There is one known limitation that you might to be aware of: You cannot extend an editor descriptor that already has a UIHint defined. So if you want to extend ContentReferences hinted with for example a “Page”, you cannot override this behavior direcly (though this can be simply archieved which will be content for another blog post).
Comments