Skip to content

Instantly share code, notes, and snippets.

@mattiasnorell
Last active August 16, 2017 15:26
Show Gist options
  • Save mattiasnorell/0daf1d851358ecb01f8c to your computer and use it in GitHub Desktop.
Save mattiasnorell/0daf1d851358ecb01f8c to your computer and use it in GitHub Desktop.
Remove all buttons from a EPiServer CMS 7 XHTML / TinyMCE editor and add only the buttons you want to use.
using EPiServer.Core;
using EPiServer.Editor.TinyMCE;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Shell.ObjectEditing.EditorDescriptors;
using Site.EPiServerAdmin.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Site.EPiServerAdmin.EditorDescriptors
[EditorDescriptorRegistration(TargetType = typeof(XhtmlString), EditorDescriptorBehavior = EditorDescriptorBehavior.PlaceLast)]
public class EditorButtonEditorDescriptor : EditorDescriptor
{
public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
{
var attributeValue = metadata.Attributes.OfType<EditorButtonsAttribute>().SingleOrDefault();
if (attributeValue == null) return;
var model = (PropertyData) metadata.Model;
var settings = (TinyMCESettings)(model).GetSetting(typeof(TinyMCESettings));
settings.ToolbarRows.Clear();
settings.ToolbarRows.Add(attributeValue.ToolbarRow);
var options = new TinyMCEInitOptions(TinyMCEInitOptions.InitType.EditMode, settings, (IContent)null);
metadata.EditorConfiguration["settings"] = options.InitOptions;
base.ModifyMetadata(metadata, attributes);
}
}
}
using EPiServer.Editor.TinyMCE;
using System;
namespace Site.EPiServerAdmin.Attributes
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class EditorButtonsAttribute : Attribute
{
private ToolbarRow _toolbarRow = new ToolbarRow();
public EditorButtonsAttribute(params string[] toolbarRowButtons)
{
foreach (var toolbarRowButton in toolbarRowButtons)
{
_toolbarRow.Buttons.Add(toolbarRowButton);
}
}
public ToolbarRow ToolbarRow
{
get { return _toolbarRow; }
set { _toolbarRow = value; }
}
}
}
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Shell.ObjectEditing;
using Site.EPiServerAdmin.Attributes;
using Site.EPiServerAdmin.Constants;
using System.ComponentModel.DataAnnotations;
namespace Site.Models.Blocks
{
[DefaultContentType(
DisplayName = "Testblock",
Description = "A block used to test things.",
GUID = "226F3E63-4DB5-4546-AFFA-B9C4A9C6BAC6")]
[DefaultImageUrl]
public class TestBlockModel:BlockData
{
[EditorButtons(new[] { "bold" })]
[Display(Name = "Main contents", GroupName = GroupNames.Default)]
public virtual XhtmlString MainBody { get; set; }
}
}
@JayWilk
Copy link

JayWilk commented Aug 16, 2017

Doesn't appear to work with newer versions of EPiserver (CMS 10+). It just rendered an empty readonly text area for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment