I have a Block type that allows nested blocks for structuring block layouts.
[ContentType(DisplayName = "Row Layout Block")]
public class RowLayoutBlock : BlockData
{
[Display(Name = "Blocks")]
[AllowedTypes(typeof(IRowLayoutItemBlock))]
public virtual ContentArea Items { get; set; }
}
IRowLayoutItemBlock contains a flag that tells the front-end that this block should be handled a little bit differently.
public interface IRowLayoutItemBlock : IContentData
{
bool IsRowLayoutItem { get; set; }
}
I don't want this flag to be visible/editable in the CMS editor and I've added the [Ignore] attribute on the inherited member.
[ContentType(DisplayName = "Image Block")]
public class ImageBlock : BlockData, IRowLayoutItemBlock
{
[Ignore]
public virtual bool IsRowLayoutItem { get; set; }
}
To set this flag I've subscribed an event handler on SavingContent and CreatingContent
context.ConfigurationComplete += delegate
{
var contentEvents = context.StructureMap().GetInstance<IContentEvents>();
contentEvents.SavingContent += (sender, args) => RowLayoutBlockContentEventListener.SetIsRowLayoutItemOnItems(args);
contentEvents.CreatingContent += (sender, args) => RowLayoutBlockContentEventListener.SetIsRowLayoutItemOnItems(args);
};
public class RowLayoutBlockContentEventListener
{
public static void SetIsRowLayoutItemOnItems(ContentEventArgs args)
{
if(args.Content is RowLayoutBlock rowLayoutBlock && rowLayoutBlock.Items?.Items != null)
{
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
foreach (var item in rowLayoutBlock.Items.Items)
{
if (contentRepository.TryGet<IRowLayoutItemBlock>(item.ContentLink, out var rowLayoutItem))
{
rowLayoutItem.IsRowLayoutItem = true;
}
}
}
}
}
This all work perfectly until I do a restart of IIS when all the flags are unset/set to false for some reason.
Is there a better way to do this or am I just missing something?