Try our conversational search powered by Generative AI!

Problem with LinkItemCollection in a nested block

CH
CH
Vote:
 

Hi,

I have a class structure involving two nested blocks, where the outer block contains an IList of the inner block type. The inner block contains a LinkItemCollection.

In the editor UI, when I add an instance of the outer block to a ContentArea, then add two instances of the inner block to the outer block and then add links to the LinkItemCollection in both instances, it works at first. Both inner block instances has links when I preview the changes. If I then go back to the editor, add another link to one of the inner block instances and publish the changes, the other instance is emptied of all its links.

Is this a bug or is it not supported to have a LinkItemCollection in an inner block?

public class OuterBlock : BlockData
{
    public virtual IList<InnerBlock> InnerBlocks { get; set; }
}

public class InnerBlock : BlockData
{
    public virtual LinkItemCollection Links { get; set; }
}
#302777
Jun 01, 2023 9:30
Vote:
 

Hi CH,

Maybe I am missing something here, but why do you have a list for a list of links? This might be just simplified for explanation.

I would have used a ContentArea rather than an IList on the OuterBlock.

I see you are using the new way of doing lists as outlined here .

You could say this is a bug however, speak to any developer out there and they will warn you when nesting blocks inside of blocks as the editing experience will be greatly impacted.

Paul

#302799
Edited, Jun 01, 2023 16:06
CH
Vote:
 

Hi Paul,

Thank you for replying. As you suggest, I actually used a ContentArea in the OuterBlock at first. The reason that I changed to this construct instead was that we wanted to completely hide InnerBlock from editors and only make it possible to create them from within OuterBlock in the UI.

My first thought was to decorate InnerBlock with ContentType(AvailableInEditMode = false), but that would also have lead to not being able to select the InnerBlock type in the ContentArea of OuterBlock. So by changing ContentArea to IList in the OuterBlock I was still able to create InnerBlocks from within OuterBlock. That solution did however give me this problem with the LinkItemCollection not being saved correctly.

I have also tried another approach to hide InnerBlock from editors using the technique described by Paul Gruffydd in this answer, for a question that is very much like my scenario  https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2015/9/hide-blocktype-from-editmode-but-available-when-combined-with-allowedtypes/#203888.

Using this technique I only succeed in hiding the block type in the global assets panel however. It is still available in ContentAreas in various page and block types. I would like it to be available only in the ContentArea in OuterBlock. I cannot think of any other way than to decorate every ContentArea in the solution with [AllowedTypes(RestrictedTypes = new[] { typeof(InnerBlock) })], which would be a maintenance headache.

Any suggestions on an alternative way to solve this would be highly appreciated.

CH

#302840
Jun 02, 2023 9:10
Vote:
 

Hi CH,

The only alternative I can think of is to change the InnerBlock to a standard POCO, and remove the LinkItemCollection.

You would then need to explicity declare the properties you would like e.g.

public class InnerBlock
{

public string Title{get;set;}
public string Name {get;set;}
public url Link {get;set;}
public contentreference{get;set;}
...
}

This would possibly even give you some degree of flexibility.

Paul

#302961
Edited, Jun 05, 2023 8:09
CH - Jun 14, 2023 8:58
Thank you for the workaround suggestion Paul. I will go ahead and do it that way.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.