Recently I had to do a custom implementation for an RTE dropdown list in Sitecore 9. I used @jammykam’s example to get started. This was done in a previous version of Sitecore. You can find it here. Granted the implementation did not change too much between versions, but if you are searching for a way to do it in Sitecore 9 this should help since there is at least one minor difference I found.
So, to not disturb the out of box RTE profiles I duplicated an existing one.
I created a custom dropdown button in the part of the RTE toolbar I would like to have the button available.
To tie the Rich Text Profile with its own EditorConfiguration pipeline a Configuration Type was added to the profile.
In the Visual Studio solution, I created the following which will define the button, add CSS and add JavaScript.
The InsertToken.xml contains the following:
-
<? xml version = “1.0”
-
encoding = “utf-8” ?> < control xmlns: def = “Definition”
-
xmlns = “http://schemas.sitecore.net/Visual-Studio-Intellisense” > < RichText.InsertToken > span.InsertToken {
-
background – image: url(‘/sitecore/shell/Themes/Standard/Images/Editor/WebResource.png’) !important;
-
} < /RichText.InsertToken> < /control>
The InsertToken.js contains the following. Telerik controls were used in later versions of Sitecore including 9 instead of the RadEditor controls.
-
Telerik.Web.UI.Editor.CommandList[“InsertToken”] = function(commandName, editor, args) {
-
var val = args.get_value();
-
editor.pasteHtml(val);
-
args.set_cancel(true);
-
};
-
jQuery(document).ready(function($) {
-
loadCSS = function(href) {
-
var cssLink = $(“<link rel=’stylesheet’ type=’text/css’ href='” + href + “‘>”);
-
$(“head”).append(cssLink);
-
};
-
loadCSS(“/sitecore/shell/Controls/Rich Text Editor/InsertToken/InsertToken.css”);
-
});
The InsertToken.css contains the following. The reason for this CSS code is so that the button in the RTE appears correctly. @Ja
span.InsertToken {
-
background – image: url(‘/temp/iconcache/office/16×16/document_json.png’) !important;
-
}
Under Pipelines\RichTextEditor an override to the EditorConfiguration was created.
You can use DotPeek or another decompiler to get the code to override, but the main thing you want to change is the following in this case. Note the ItemsPerRow kept the column values in one single line.
-
case “Html Editor Custom Drop Down Button”:
-
var editorSplitButton1 = new EditorSplitButton {
-
Name = str1, ShowIcon = true, ItemsPerRow = “1”, ImageUrl = “/temp/iconcache/office/24×24/document_json.png”
-
};
-
EditorSplitButton editorSplitButton2 = editorSplitButton1;
-
SetProperties((EditorTool) editorSplitButton2, obj);
-
EditorConfiguration.SetChildren(editorSplitButton2.Items, obj);
-
toolbar.Tools.Add((EditorToolBase) editorSplitButton2);
-
continue;
The list of values is fed from Sitecore. The token item contains a Description and TokenValue field.
SetChildren is called which in turns calls GetTokenList to retrieve the values and set the dropdown.
-
private static void SetChildren(EditorDropDownItemCollection items, Sitecore.Data.Items.Item parent) {
-
Assert.ArgumentNotNull((object) items, “items”);
-
Assert.ArgumentNotNull((object) parent, “parent”);
-
foreach(Sitecore.Data.Items.Item obj in parent.Children) {
-
if (parent.Name == “Insert Token” && obj.TemplateName == “Html Editor List Item”) {
-
items = GetTokenList(items);
-
} else {
-
if (obj.TemplateName == “Html Editor List Item”) {
-
items.Add(obj[“Header”], obj[“Value”]);
-
}
-
}
-
}
-
}
-
private static EditorDropDownItemCollection GetTokenList(EditorDropDownItemCollection items) {
-
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase(“master”);
-
Item parentTokenFolder = master.GetItem(“/sitecore/content/Tokens”);
-
if ((parentTokenFolder == null) || (!parentTokenFolder.HasChildren)) {
-
return items;
-
}
-
foreach(Item token in parentTokenFolder.Children) {
-
items.Add(token.Fields[“TokenValue”].Value + ” – “ + token.Fields[“Description”].Value, token.Fields[“TokenValue”].Value);
-
}
-
return items;
-
}
After the changes you should see the RTE button in the editor and selecting it should insert the text that was predefined.
Pingback: Adding Custom Dropdown Button in #Sitecore 9 to Content Editor Ribbon | Sitecore Runner | Rob Reilley | Sitecore MVP
Hi,
I am using Sitecore version 10.2 with docker containers.
When implementing these steps it is required to override the setupToolbar method and it needs to reference Telerik dlls
Is there a possibility to add values to custom dropdown list in Rich Text without referring Telerik.Web.Ui dll since it is not open source
LikeLike
Hi Nandhini. I am currently using the same version with Docker as well. I am not sure on your question since it has been a while since I had to overwrite the RTE field. Maybe try using Dotpeek to see if you can look into the dll of Telerik to see if you can duplicate any functionality.
LikeLike