Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Linq;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.EntityViews;
using Sitecore.Framework.Pipelines;
using Sitecore.Services.Examples.Entities.Entities;
namespace Sitecore.Services.Examples.Entities.Pipelines.Blocks
{
[PipelineDisplayName("PopulateBrandsViewActionsBlock")]
public class PopulateBrandsViewActionsBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
private readonly IFindEntitiesInListPipeline _findEntitiesInListPipeline;
public PopulateBrandsViewActionsBlock(IFindEntitiesInListPipeline findEntitiesInListPipeline)
{
_findEntitiesInListPipeline = findEntitiesInListPipeline;
}
public override async Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
{
var name = entityView?.Name;
if (string.IsNullOrEmpty(name) || !entityView.Name.Equals("All Brands", StringComparison.OrdinalIgnoreCase))
{
return await Task.FromResult(entityView);
}
var findEntitiesInListArgument = await _findEntitiesInListPipeline.Run(new FindEntitiesInListArgument(typeof(Brand), CommerceEntity.ListName<Brand>(), 0, 9), context);
var policy = entityView.GetPolicy<ActionsPolicy>();
var actions = policy.Actions;
var entityActionView = new EntityActionView()
{
Name = "AddBrand",
DisplayName = "Add Brand",
Description = "Adds a Brand",
IsEnabled = true,
EntityView = "Details",
Icon = "add"
};
actions.Add(entityActionView);
var entityActionViews = policy.Actions;
var empty = new EntityActionView()
{
Name = "RemoveBrand",
DisplayName = "Remove Brand",
Description = "Removes a Brand"
};
var exists = findEntitiesInListArgument?.List.Items.Any() ?? false;
empty.IsEnabled = exists;
empty.EntityView = string.Empty;
empty.RequiresConfirmation = true;
empty.Icon = "delete";
entityActionViews.Add(empty);
return await Task.FromResult(entityView);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.