"MySpace" the Content Editor - Customizing the Content Editor via the Rules Engine Part 1

Tuesday, November 15, 2016 @ 09:00
MySpace Image

By: Eric Stafford, Sitecore Developer – Joking! Although it's totally possible to "MySpace" the Content Editor, there are better ways to customize the it.

A couple weeks back a colleague asked me what I thought about the possibility of changing the display name of a field on a case by case basis. As we discussed the usefulness of this idea, I had already started researching. With one of my favorite development tools “JetBrain’s DotPeek” open, I searched the Sitecore kernel and discovered that not only is my colleague’s idea possible, but implementing it won’t create future Sitecore upgrade headaches.

As it turns out, customizing the Content Editor is as simple as decompiling and editing the EditorFormatter and utilizing the “renderContentEditor” pipeline.

My colleague began implementing his approach that involves YAML while I began my rules engine approach.

Customizing the Content Editor via the Rules Engine can be a very powerful tool. It can allow developers to create single field templates with names descriptive of their general use. These can be inherited on Data and Page Templates allowing for rapid modeling, etc, all while keeping the content author’s editing experience as straightforward as possible.

Customizing the content editor shouldn’t be relied on as a crutch for making poor architectural decisions. However, it can be very useful with the dreaded old Sitecore implementation where every page template contains every field possible with or without general field names.

I’ve inherited a few of these sites over the last 10 years and as frustrating it is to maintain and develop new features. It is also extremely frustrating for an author to content manage. Often times the client wants these templates simplified but sometimes the effort involved is out of the client’s budget. This approach doesn't fix the underlying issue, but it can be used as a quick fix to make their authoring lives less stressful.

Creating the Rule Items

Create a Tag
Right click on the Tags folder (/sitecore/system/Settings/Rules/Definitions/Tags) and create the new tag "Customize the Content Editor". This tag will be used to tie the Conditions and actions under a Rule Element Folder to the rules under a Rule Context Folder.

Create a Rule Element Folder
Create an Element folder under the "/sitecore/system/Settings/Rules/Definitions/Elements/ node". This element will hold our custom actions and any custom conditions we may want to add in the future. Once this folder is created, navigate to the "/sitecore/system/Settings/Rules/Definitions/Elements/Customize the Content Editor/Tags/Default" item and select the "Customize the Content Editor" tag.

Creating the Actions
For this blog post we are going to focus on the Fields. The link to the solution below will also have actions that are Section specific.

Display Field Name As
In the Data section -> Text field, add: Display [FieldName,,,Field Name] as [NewName,,,value]
In the Script section-> Type field, add the fully qualified name for the action:
Paragon.Sc.ContentEditor.Rules.Actions.SetFieldNames,Paragon.Sc.ContentEditor

Set Field Short Description
In the Data section -> Text field, add: Set [FieldName,,,Field Name]'s Short Description as [Text,,,value]
In the Script section-> Type field, add the fully qualified name for the action:
Paragon.Sc.ContentEditor.Rules.Actions.SetShortDescription,Paragon.Sc.ContentEditor

Hide Field
In the Data section -> Text field, add: Hide [FieldName,,,Field]
In the Script section -> Type field, add the fully qualified name for the action:
Paragon.Sc.ContentEditor.Rules.Actions.HideFields,Paragon.Sc.ContentEditor

Creating the Rules Context Folder
Right click on the "/sitecore/system/Settings/Rules" item and insert the Rules Context Folder and give it the name "Customize the Content Editor".

Next, navigate to the "/sitecore/system/Settings/Rules/Customize the Content Editor/Tags/Default" item. This is where we tie the conditions and actions to our rules. Select the "Customize the Content Editor" tag for our custom actions. We also need some conditions, select the "Item Information" and "Item Hierarchy" tags.

The Code

I've removed lines of code from the code snippits to reduce the length of this blog. The full code is available for download below.

Create an Item for the Mapping
This will consist of an Enum and the Concrete type.

Extension Methods
I created a couple SIMPLE extension methods to keep the code a little cleaner (Ew, I know). These will be used in the actions.

Create the Rule Context and Rule Processor
The Rule Context passes the list of Mapped Items along to the Rule Processor and Actions. The Rule Processor gets called in the EditorFormatter and runs the rules. If the selected conditions = true, then the Mapped Item list gets populated with the values defined in the rule's actions.

Create the Actions

Create the RenderCustomizedContentEditor Pipeline

The Config:

And finally, the EditorFormattor.cs
I've included only the customized code.


Example of the Rule in Action
Before the rule.
Before the rule.

The Rule
The Rule

After the rule was applied.
After the rule was applied.


Part two of the blog series will focus on adding the ability to change the titles or hide items in mass.

Haven't read the other chapters?
Part 2 - "MySpace" the Content Editor - Customizing the Content Editor via the Rules Engine
Part 3 - "MySpace" the Content Editor - Customizing the Content Editor via the Rules Engine

To download the solution, click here.


Follow me on Twitter: @SitecoreEric