Sitecore Insiders

Code Editor for custom field using SPEAK & CodeMirror – Part 2

Part 2 – Create a custom field type based on Multi-line Text

Creating a new field type is not that simple unless you inherit from an existing one, and that’s what I’m going to show. We could just add this feature to the existing Multi-Line Text but that’s not how you should do things in Sitecore, so, here you will learn the quickest way to create a custom field type.

Basically, we will need an item in Core database to define the new field, a C# class supporting it and a config file to map the field to the class. I’m keeping this part simple to separate what is the field code and configuration from the dialog related logic.

Btw, check here the previous step.

The custom field type item

  • Switch to Core database.
  • Go to /sitecore/system/Field types, you will find folders according to the group of field types displayed in field type selection on template builder.
  • In this case we are extending Multi-line Text, so let’s copy /Simple Types/Multi-Line Text to /Custom Types and call it “Code”.
    This item is based on “Template field type” and the name you set will be the one to appear in the field type selection when creating templates.
  • Only the “Control” field is fulfilled here and this is composed by a prefix, defined by you, and the name of the class that will represent your new field. Didn’t try to write the prefix in non lowercase but it’s like that everywhere, so, I recommend it to be lowercase.
    Let’s write the prefix:ClassName in “Control” field as “codeeditor:CodeField”.

The code & config

Here we are just going to write the base code and the config file. We are going back to this in the next chapter.

  • Create a C# class called CodeField inheriting from Sitecore.Shell.Applications.ContentEditor .Memo with just the constructor.
    The Memo class is the one supporting Multi-Line Text field, in this case we will use it and extend it.
using Sitecore.Shell.Applications.ContentEditor;

namespace SitecoreInsiders.Shell.CodeEditor.Controls
{
    public class CodeField : Memo
    {
        public CodeField()
        {
        }
    }
}
  • Create a config file with the content below to define the class and the prefix.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <controlSources>
      <source mode="on" namespace="SitecoreInsiders.Shell.CodeEditor.Controls" assembly="SitecoreInsiders.Shell.CodeEditor" prefix="codeeditor" />
    </controlSources>
  </sitecore>
</configuration>

In the next chapter, I’m going to show how to add a menu button and call the dialog you created.

Click here to go to the next step.

Source Code

Still in doubt with something? Check here. Have in mind that this is the final product.

https://github.com/sitecoreinsiders/SitecoreRepo/blob/v1.0/SitecoreInsiders.Shell.CodeEditor/App_Config/Include/Z.SitecoreInsiders.Shell/SitecoreInsiders.Shell.CodeEditor.config

https://github.com/sitecoreinsiders/SitecoreRepo/blob/v1.0/SitecoreInsiders.Shell.CodeEditor/Controls/CodeField.cs

Bruno Nunes

Sitecore developer since 2015, passed by 7.5, 8.2, 9.1, next step will be the 10!
Self-motivated to bring new capabilities to Sitecore and to adapt it for every business case.
Currently working on Noesis as Sitecore Architect / Tech Lead.

Add comment

Bruno Nunes

Sitecore developer since 2015, passed by 7.5, 8.2, 9.1, next step will be the 10!
Self-motivated to bring new capabilities to Sitecore and to adapt it for every business case.
Currently working on Noesis as Sitecore Architect / Tech Lead.