Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 444
Raskolnikoov Avatar asked Oct 23 '25 01:10

Raskolnikoov


2 Answers

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");    
    }
  }
like image 132
Frederik Vig Avatar answered Oct 24 '25 14:10

Frederik Vig


As of EpiServer 8 theres a new attribute called [AllowedTypes]. This is now the best way of restricting blocks. It overcomes a lot of the limitations of [AvailableContentTypes]. When you drag blocks into a content area the validation actually works.

An example code snippet would be

[AllowedTypes(new []{ typeof(SlideBlock) })]
public virtual ContentArea Slides { get; set; }

Theres a good code example here How To Restrict The Blocks Allowed Within A Content Area Episerver

Also this one on EpiWorld http://world.episerver.com/blogs/Ben-McKernan/Dates/2015/2/the-new-and-improved-allowed-types/

like image 38
Jon Jones Avatar answered Oct 24 '25 15:10

Jon Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!