Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Restrict which blocks that can be used in TinyMce.

Vote:
 

Is there a way to restrict which types of block are allowed in TinyMce (like you can do with AllowedTypes-attribute on ContentArea property)?

We have a project which contains two sites and I want to specify which blocks are available on Site A and which blocks are available on Site B.

#322602
Edited, May 24, 2024 10:56
Vote:
 

If you have a different role per site, you can use this to restrict who can see each block. 

Blocks can have an Access attribute like so:

[ContentType(DisplayName = "Test Block", GUID = "xxxx-xxxx")]
[Access(Roles = "SiteBRole")]
public class TestBlock : BlockData {
}

This'll filter out the block for everyone not in the SiteBRole.

If you want to filter it per site, you can implement a ContentTypeAvailabilityService  as suggested here.
It'll allow you to filter the list of pages and blocks per your own criteria.

#322718
May 27, 2024 5:42
Vote:
 

This can be done but you need to write a validator for the XhtmlString type (in combination with Arjan Paauw's answer).

Here's a brilliant blog post on it, https://surjitbharath.wordpress.com/2017/02/02/episerver-10-restrict-any-block-types-on-xhtmlstring-property-using-the-validation-attribute/

Codesnippet from Surjit's Dev Blog

Property

[Display(Name = "Text", GroupName = SystemTabNames.Content)]
[XHtmlStringAllowedBlockTypes(new[] { typeof(TextBlock), typeof(ImageBlock) })]
public virtual XhtmlString RichText { get; set; }

Attribute

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;
  }
}
#322719
Edited, May 27, 2024 7:15
Vote:
 

Thanks for the advice guys. I implemented a ContentTypeAvailabilityService  that did the trick for me :)

#322720
May 27, 2024 7:44
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.