Multiple Selection of Blocks


I've been searching everywhere for this example, so far haven't found anything. Hence the post here.  Is there a way to have a list of checkboxes to select a particular block type from that list, from the editor?

Thanks in advance.

Jul 07, 2017 21:12

There's nothing really built in.

I would suggest that you either have an IList Property and use my SelectionFactory for content with a SelectOneAttribute.

[SelectOne(SelectionFactoryType = typeof(ContentSelectionFactory<MyBlock>))]
public virtual ContentReference MyContentReference { get; set }

Otherwise a simple way to do it (but not so elegant) is to have a string property, use the SelectMany attributes with my SelectionFactory for content.

[SelectMany(SelectionFactoryType = typeof(ContentSelectionFactory))]
public virtual string MyTeasers { get; set; }

However you want to remove the default value on row 22.

allContent.Add(new SelectItem());

The value of the property will be a comma separated string with ContentReference IDs "123,456".

You will yourself split that string by comma, parse the PageReference for each string and load each Block.

var myTeasers = CurrentPage.MyTeasers;
var teaserArray = myTeasers.Split(new [] { ',' }, StringSplitOptions.None);

var contentLinks = teaserArray.Select(x => new PageReference(x));

var blocks = contentLoader.GetItems(contentLinks, LanguageSelector.AutoDetect());

The code is just quickly thrown together so some adjustments might be needed to suit exactly what you want.

Edited, Jul 08, 2017 15:55
* 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.