Quick mappning of CategoryPointer?

With a PointerTypeConstants.WebsitesPage you can quickly map this type to a model or link using someting like:

var pageList    = blockModel.Fields.GetValue<IList<PointerItem>>(BlockFieldNameConstants.PageLinkList)?.OfType<PointerPageItem>().ToList().Select(x => x.MapTo<LinkModel>()).Where(x => x.AccessibleByUser).ToList() ?? new List<LinkModel>();

But for PointerTypeConstants.ProductsCategory I can’t find an Automapping for either CategoryModel nor LinkModel.

I have solved this using a List of Guids and the CategoryService, but I’m guessing there is a built-in way/mapping of converting a list of category Guids to LinkModels or CategoryModels that I just can’t find?

This is what I want to resolve as links to the viewModel:

new FieldDefinition<BlockArea>(BlockFieldNameConstants.CategoryLinkList, SystemFieldTypeConstants.Pointer)
				{
					Option = new PointerOption { EntityType = PointerTypeConstants.ProductsCategory, MultiSelect = true}
				},

Is this a good way of doing it?

			foreach (var item in categoryIds)
			{
				CategoryModel categoryModel = item.EntitySystemId.MapTo<CategoryModel>();
				LinkModel categoryLinkModel = new LinkModel();
				if (categoryModel != null)
				{
					if (categoryModel.Category != null && categoryModel.Category.IsPublished(websiteSystemId, channelSystemId) || true)
					{
						categoryLinkModel.Text = categoryModel.Category.Fields.GetValue<string>("_name", CultureInfo.CurrentUICulture);
						categoryLinkModel.Href = _urlService.GetUrl(categoryModel.Category) ?? "/not-set-in-admin";

						categoryList.Add(categoryLinkModel);
					}
				}
			}

Litium version: 7.1

When we have the PointerTypeConstants.WebsitesPage the item is of the type PointerPageItem and then we know with the mapping that it is a page that we should create the link to. When we link to a category (or any of the other entity that exists) the type is PointerItem and then we don’t know during the mapping what type the PointerItem.SystemId to be able to convert that to a link.

In this case you can execute something like this to get an one-liner.

blockModel.Fields.GetValue<IList<PointerItem>>(BlockFieldNameConstants.CategoryLinkList)?.Select(x => x.MapTo<Category>().MapTo<LinkModel>()).Where(x => x.AccessibleByUser).ToList() ?? new List<LinkModel>();

in your suggestion is not needed and you can remove that if-statement (it will always result in a true).

1 Like