Is there any way to find out what specific block types there are on a page?
I’m asking this because I would like a specific thing to occur with a productitem, if the said item resides in a productblock on the page.
I want to access what block type it is from the productitemviewmodelbuilder.
You can use the PageService to get the page and its containing Blocks container and then you can check the type of each Block inside the container items.
Thanks for the reply Steve,
However I can’t seem to see the specific blocktype.
I’m using var currentPage = _requestModelAccessor.RequestModel.CurrentPageModel;
to get the current page.
From this I can get the blocks container, when I look at the blockitems, the only thing I can see is the that key is “Main” and that the container is a generic block.blockmodel.
However I know that there is a productblockmodel with four products in it
The logic that matches FieldTemplateSystemId to BlockController and View are somewhere inside
the BlockHelperExtension method Block() right? Am also trying to match this, but wondering about how to moast effenently do this mapping on my own.
We fetching the field template (from the field template system id) and then parsing the TemplatePath-property to get the controller/action combination.
Parsing logic to find the controller and action is as below
internal static class TemplateExtensions
{
internal static TemplateControllerAction ParseTemplate(string templateFileName)
{
if (string.IsNullOrWhiteSpace(templateFileName) || templateFileName.IndexOf("MVC:", StringComparison.OrdinalIgnoreCase) == -1)
{
return null;
}
var templateDefaults = templateFileName.Split(':').Skip(1).ToArray();
if (templateDefaults.Length < 1)
{
return null;
}
var controllerType = Type.GetType(templateDefaults[0], true, true);
var controllerName = controllerType?.Name.Replace("Controller", "");
var requestedAction = templateDefaults.Length > 1 ? templateDefaults[1] : "Index";
return new TemplateControllerAction
{
Controller = controllerName,
Action = requestedAction
};
}
internal class TemplateControllerAction
{
/// <summary>
/// Gets or sets the action.
/// </summary>
/// <value>The action.</value>
public string Action { get; set; }
/// <summary>
/// Gets or sets the controller.
/// </summary>
/// <value>The controller.</value>
public string Controller { get; set; }
}
}
Thanks! Now the one problem that remains is that automapping dont seam to work from an api call? I get all the BlockItemLink of my the startPage but the automapper return null on every blockModel, whare is missing? Do I need to act as system?
[HttpGet]
public IHttpActionResult Get(Guid pageId, string types)
{
List<JsonBlock> returnList = new List<JsonBlock>();
var page = _pageService.Get(pageId);
var blockTypes = types.Split(',');
foreach (var blockContainer in page.Blocks)
{
foreach (var blockItem in blockContainer.Items.OfType<BlockItemLink>())
{
var blockModel = blockItem.BlockSystemId.MapTo<BlockModel>();
//var blockTemplate = _fieldTemplateService.Get<FieldTemplateBase>(blockModel.FieldTemplateSystemId);
//if (blockTemplate.FieldGroups.Any(x => x.Id == "Content"))
//{
// var blockItems = block.GetValue<IList<MultiFieldItem>>(BlockFieldNameConstants.Content);
// if (blockItems != null)
// {
// result.AddRange(blockItems.Select(c => c.MapTo<ContentBlockItemViewModel>()));
// }
//}
//var jsonBlock = new JsonBlock { Type = "BlockItemLink", Container = blockContainer.Id, Data = blockTemplate };
//returnList.Add(jsonBlock);
}
}
return Json(returnList);
}