Search code examples
episerverepiserver-7

Restrict Blocks in ContentArea


I'm having a issue restricting what kind of Block to be inserted in a ContentArea. What I want is that the SliderBlock's ContentArea property can only have insertion of a SlideItemBlock.

[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
}

Or is this the wrong way to achive what I'm trying to restrict for the editor to not drag and drop wrong block types?

As of now, I can create a SliderBlock and insert a SlideItemBlocks in it. If I then insert the created SliderBlock in a new SliderBlock I get a forever and ever loop and It breaks the site. This is what I'm trying to control.


Solution

  • If you´re using EPiServer 7.5 restricting which blocks you can use in a content area is built in. For details take a look at this blog post: Restricting the allowed types in a content area.

    Code example from the blog post:

      [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");    
        }
      }