Skip to content

Instantly share code, notes, and snippets.

@websterian
Created October 10, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save websterian/b2eadbd2dd0a1856049eb705ff9cee69 to your computer and use it in GitHub Desktop.
Save websterian/b2eadbd2dd0a1856049eb705ff9cee69 to your computer and use it in GitHub Desktop.
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