Search code examples
episerver

Nested block types in Episerver


I am about to build a block for creating content tables in Episerver.

I would like this block to have a ContentArea which in turn will contain only blocks of type TableRowBlock (so that I can have an arbitrary number of rows).

If I create a block type called TableBlock and another one called TableRowBlock, they will both be visible when an editor adds a new block.

Since TableRowBlock only makes sense within a TableBlock, I would like to hide it so that it is only visible when adding a block to the ContentArea property of a TableBlock.

How can I do this?


Solution

  • What you're asking isn't supported out-of-the-box, I'm afraid.

    However, you could:

    1. Add an [AllowedContentTypes] attribute to your ContentArea property for TableBlock, and specify the TableRowBlock type as the allowed type. That way the editor won't have to select block type when clicking "Add new block" in the content area editor.
    2. Customize which content type(s) are suggested when new content is created by creating your own IContentTypeAdvisor which would suggest TableRowBlock when TableBlock is being edited:

    [ServiceConfiguration(typeof(IContentTypeAdvisor))] public class ContentTypeAdvisor : IContentTypeAdvisor { public IEnumerable<int> GetSuggestions(IContent parent, bool contentFolder, IEnumerable<string> requestedTypes) { // Suggest relevant content types } }

    Full example available here.