I couldn’t find a similar solution online as I believe there is an emerging pattern where we are now instructing clients to drag EPiServer Blocks into Rich Text editors.
Requirement:
As an Editor I want to limit certain blocks types from being dragged into the rich text editor
Solution #1:
Apply this attribute to any of your xhtml properties to enable to validation functionality
[Display(Name = "Text", GroupName = SystemTabNames.Content)] [XHtmlStringAllowedBlockTypes(new[] { typeof(TextBlock), typeof(ImageBlock) })] public virtual XhtmlString RichText { get; set; }
Implement the following attribute where your other validation attributes reside.
public class XHtmlStringAllowedBlockTypes : ValidationAttribute { private readonly Type[] allowedTypes; public XHtmlStringAllowedBlockTypes(Type[] allowedTypes) { this.allowedTypes = allowedTypes; } protected override ValidationResult IsValid(object value, ValidationContext context) { var contentData = context.ObjectInstance as IContentData; if (contentData != null && contentData.Property[context.MemberName].Value is XhtmlString) { var richTextProperty = (XhtmlString)contentData.Property[context.MemberName].Value; foreach (ContentFragment fragment in richTextProperty.Fragments.Where(x => x is ContentFragment)) { var content = ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContentData>(fragment.ContentLink); foreach (var allowedType in allowedTypes) { if (allowedType.IsInstanceOfType(content)) { return new ValidationResult(string.Format("You cannot add {0} to {1}", content.GetType(), context.MemberName)); } } } } return ValidationResult.Success; } }
References:
http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Content/Validation/