How to get specific block from BlockGrid model

Thanks Justin :raising_hands:

I couldn’t get it to work properly (but you did say it wasn’t tested) but it got me on the right path to write the needed extensions – basically this:

public static IEnumerable<BlockGridItem> Flatten(this BlockGridArea? area) {
	if (area is not null) {
		foreach (var item in area) {
			yield return item;
		}
	}
}

public static IEnumerable<BlockGridItem> Flatten(this BlockGridModel? grid) {
	if (grid is null) return Enumerable.Empty<BlockGridItem>();

	return grid
		.SelectMany(item =>
			new[] { item }
				.Concat(item.Areas.SelectMany(area => area.Flatten()))
		);
}
	
public static IEnumerable<BlockGridItem> GetRaptors(this IPublishedContent cage) {
	return cage.Value<BlockGridModel>("Dinosaurs").Flatten().Where(d => d.Content.ContentType.Alias == "raptor");
}

So thanks — I can now warn the visitors if there are raptors close by :rofl:

/Chriztian

1 Like