在ContentArea中限制块
我遇到了限制要在ContentArea中插入哪种块的问题。我想要的是,SliderBlock的ContentArea属性只能插入一个SlideItemBlock。在ContentArea中限制块
[ContentType(...)] public class SlideItemBlock : BlockData
{
[Required]
Display(Name = "Image")]
public virtual string Image { get; set;}
}
[ContentType(...)]
public class SliderBlock : BlockData
{
[Required]
[Display(Name = "Slides")]
public virtual ContentArea Slides { get; set; }
//Should only accept insertion of SlideItemBlock
}
或者这是错误的方式来实现我想限制编辑器不拖放错误的块类型?
截至目前,我可以创建一个SliderBlock并在其中插入一个SlideItemBlocks。如果我然后将创建的SliderBlock插入到一个新的SliderBlock中,我会得到一个永不停歇的循环,并打破该网站。这是我想要控制的。
回答:
如果you're使用EPiServer 7.5限制哪些块,你可以在内容区建成在使用细节来看看这个博客帖子:Restricting the allowed types in a content area。从博客文章
代码示例:
[EditorDescriptorRegistration(TargetType = typeof(ContentArea), UIHint = "Gallery")] public class ImageGalleryEditorDescriptor : EditorDescriptor
{
public ImageGalleryEditorDescriptor()
{
// Setup the types that are allowed to be dragged and dropped into the content
// area; in this case only images are allowed to be added.
AllowedTypes = new Type[] { typeof(IContentImage) };
// Unfortunetly the ContentAreaEditorDescriptor is located in the CMS module
// and thus can not be inherited from; these settings are copied from that
// descriptor. These settings determine which editor and overlay should be
// used by this property in edit mode.
ClientEditingClass = "epi-cms.contentediting.editors.ContentAreaEditor";
OverlayConfiguration.Add("customType", "epi-cms.widget.overlay.ContentArea");
}
}
回答:
您可以向内容区属性添加验证属性以限制允许的块类型。 有关详细示例,请参阅this link。
然后使用AvailableContentTypes属性可以限制为只允许SlideItemBlock类型。 。
[Required] [Display(Name = "Slides")]
[AvailableContentTypes(Include = new []{typeof(SlideItemBlock)})]
public virtual ContentArea Slides { get; set; }
回答:
你作为弗雷德里克建议我们有以下属性我们已经创建完成这一点还没有升级到7.5呢。
using EPiServer.Core; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace xxx.Com.Core.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class OurAvailableContentTypesAttribute : ValidationAttribute
{
public Type[] Include { get; set; }
public Type[] Exclude { get; set; }
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
if (!(value is ContentArea))
{
throw new ValidationException("OurAvailableContentTypesAttribute is intended only for use with ContentArea properties");
}
var contentArea = value as ContentArea;
var notAllowedcontentNames = new List<string>();
if (contentArea != null)
{
if (Include != null)
{
var notAllowedContent = contentArea.Contents.Where(x => !ContainsType(Include, x.GetType()));
if (notAllowedContent.Any())
{
notAllowedcontentNames.AddRange(notAllowedContent.Select(x => string.Format("{0} ({1})", x.Name, x.ContentLink.ID)));
}
}
if (Exclude != null)
{
var notAllowedContent = contentArea.Contents.Where(x => ContainsType(Exclude, x.GetType()));
if (notAllowedContent.Any())
{
notAllowedcontentNames.AddRange(notAllowedContent.Select(x => string.Format("{0} ({1})", x.Name, x.ContentLink.ID)));
}
}
}
if (notAllowedcontentNames.Any())
{
ErrorMessage = "contains invalid content items :";
foreach (var notAllowedcontentName in notAllowedcontentNames)
{
ErrorMessage += " " + notAllowedcontentName + ",";
}
ErrorMessage = ErrorMessage.TrimEnd(',');
return false;
}
return true;
}
private bool ContainsType(Type[] include, Type type)
{
return include.Any(inc => inc.IsAssignableFrom(type));
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var result = base.IsValid(value, validationContext);
if (result != null && !string.IsNullOrEmpty(result.ErrorMessage))
{
result.ErrorMessage = string.Format("{0} {1}", validationContext.DisplayName, ErrorMessage);
}
return result;
}
}
}
的这种用法是那么
public class OurBlock : BlockData {
[CultureSpecific]
[Editable(true)]
[Display(Name = "",
Description = "",
GroupName = SiteConstants.GroupNames.ContentArea,
Order = 1)]
[OurAvailableContentTypes(Include = new[] { typeof(OurImageBlock) })]
public virtual ContentArea ImageContentArea { get; set; }
HTH
亚当
回答:
创建一个验证类并实现从EPiServer.validation的IValidate接口。这个验证被保留在PageData和BlockData类之外。
这应该是你在http://sdkbeta.episerver.com/SDK-html-Container/?path=/SdkDocuments/CMS/7/Knowledge%20Base/Developer%20Guide/Validation/Validation.htm&vppRoot=/SdkDocuments//CMS/7/Knowledge%20Base/Developer%20Guide/
找什么
using System.Collections.Generic; using System.Linq;
using EPiServer.Validation;
public class SliderBlockValidator : IValidate<SliderBlock>
{
public IEnumerable<ValidationError> Validate(SliderBlock instance)
{
var errors = new List<ValidationError>();
if (instance.Slides != null &&
instance.Slides.Contents.Any(x => x.GetType().BaseType != typeof (SlideItemBlock)))
{
errors.Add(new ValidationError()
{
ErrorMessage = "Only SlideItemBlocks are allowed in this area",
PropertyName = "Slides",
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.StorageValidation
});
}
return errors;
}
}
更多阅读如果你已经升级到EPI 7.5可以使用AllowedTypes注释
[AllowedTypes(new [] {typeof(SlideItemBlock)})] public virtual ContentArea Slides { get; set; }
我不知道如果您可以使用后面的解决方案自定义任何消息。有几个已知的限制
- 在页面上编辑时,限制对叠加层不起作用。这是一个已经修复的错误,将在几周内发布。
- 没有服务器验证。目前,该属性仅在UI中添加限制。我们希望能够尽快添加对服务器验证的支持,这也将使验证您的定制属性成为可能。
- 没有验证创建内容的区域局部块时。如果使用新功能局部块添加到内容领域,目前还没有当你创建新的块的内容类型的过滤。
更多的http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2013/12/Restriction-of-content-types-in-properties/
总而言之第一个解决方案是目前最好的一个。
回答:
作为EpiServer 8那里有一个称为[AllowedTypes]新的属性。现在这是限制区块的最佳方式。它克服了[AvailableContentTypes]的很多限制。当您将块拖入内容区域时,验证实际上会起作用。
的示例代码片段将
[AllowedTypes(new []{ typeof(SlideBlock) })] public virtual ContentArea Slides { get; set; }
即使世界在这里一个很好的代码示例How To Restrict The Blocks Allowed Within A Content Area Episerver
而且这一项上EpiWorld http://world.episerver.com/blogs/Ben-McKernan/Dates/2015/2/the-new-and-improved-allowed-types/
以上是 在ContentArea中限制块 的全部内容, 来源链接: utcz.com/qa/258014.html