Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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.
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;
}
}
Thanks for the advice guys. I implemented a ContentTypeAvailabilityService
that did the trick for me :)
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.