Loading...

Last updated: Oct 27 2016

Area: Episerver CMS Applies to versions: 10 and higher
Other versions:

Registering a custom editor for a specific type

You can create a page with a shared content editor for any .NET object with a generic object editing system in Episerver. This topic explains how to register a custom editor for a specific type.

Examples

The following example demonstrates how to register a custom editor. To create the actual editor, see Creating an editor widget.

using EPiServer.Core;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;

namespace CodeSamples.EPiServerShell.EditorDescriptors
{
    /// <summary>
    /// Editor descriptor that will create a checkbox to be able to edit boolean values.
    /// </summary>
    [EditorDescriptorRegistration(TargetType = typeof(bool))]
    public class BooleanEditorDescriptor : EditorDescriptor
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BooleanEditorDescriptor"/> class.
        /// </summary>
        public BooleanEditorDescriptor()
        {
            //The default editing class that will be used in forms editing mode.
            ClientEditingClass = "dijit/form/CheckBox";
        }
    }
}

You can also define settings for the editor you want to use as shown in the following example.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using EPiServer.Editor;
using EPiServer.Shell;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;

namespace CodeSamples.EPiServerShell.EditorDescriptors
{
    /// <summary>
    /// Editor descriptor that will create a select input to be able to edit float number values
    /// </summary>
    [EditorDescriptorRegistration(TargetType = typeof(float))]
    public class FloatNumberEditorDescriptor : EditorDescriptor
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyFloatNumberEditorDescriptor"/> class.
        /// </summary>
        public FloatNumberEditorDescriptor()
        {
            //The default editing class that will be used in forms editing mode.
            ClientEditingClass = "dijit/form/NumberTextBox";
        }

        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            base.ModifyMetadata(metadata, attributes);

            if (!attributes.OfType<RangeAttribute>().Any())
            {
                var constraints = new Dictionary<string, object>() 
                {
                    {"min", Double.MinValue },
                    {"max", Double.MaxValue }
                };
                //These settings will be passed to the constructor of the editor widget.
                metadata.EditorConfiguration.Add("constraints", constraints);
            }

            //Define what kind of editor we want to have, in this case we want to create an inline editor. This is used
            //when the user is in "on page editing" mode.
            metadata.CustomEditorSettings["uiWrapperType"] = UiWrapperType.Inline;
            //Specify the class for a custom editor responsible for the actual editing.
            //If not defined the default (forms) editor will be used.
            metadata.CustomEditorSettings["uiType"] = "dijit/form/NumberTextBox";
        }
    }
}

Related topics


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/