|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\commerce_promotion; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\EntityInterface; |
| 6 | +use Drupal\Core\Entity\EntityListBuilder; |
| 7 | +use Drupal\Core\Entity\EntityTypeInterface; |
| 8 | +use Drupal\Core\Entity\EntityStorageInterface; |
| 9 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Defines the list builder for coupons. |
| 14 | + */ |
| 15 | +class CouponListBuilder extends EntityListBuilder { |
| 16 | + |
| 17 | + /** |
| 18 | + * The current route match. |
| 19 | + * |
| 20 | + * @var \Drupal\Core\Routing\RouteMatchInterface |
| 21 | + */ |
| 22 | + protected $routeMatch; |
| 23 | + |
| 24 | + /** |
| 25 | + * The usage. |
| 26 | + * |
| 27 | + * @var \Drupal\commerce_promotion\PromotionUsageInterface |
| 28 | + */ |
| 29 | + protected $usage; |
| 30 | + |
| 31 | + /** |
| 32 | + * The usage counts. |
| 33 | + * |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + protected $usageCounts; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructs a new CouponListBuilder object. |
| 40 | + * |
| 41 | + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
| 42 | + * The entity type definition. |
| 43 | + * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
| 44 | + * The entity storage. |
| 45 | + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match |
| 46 | + * The current route match. |
| 47 | + * @param \Drupal\commerce_promotion\PromotionUsageInterface $usage |
| 48 | + * The usage. |
| 49 | + */ |
| 50 | + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RouteMatchInterface $route_match, PromotionUsageInterface $usage) { |
| 51 | + parent::__construct($entity_type, $storage); |
| 52 | + |
| 53 | + $this->routeMatch = $route_match; |
| 54 | + $this->usage = $usage; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritdoc} |
| 59 | + */ |
| 60 | + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { |
| 61 | + return new static( |
| 62 | + $entity_type, |
| 63 | + $container->get('entity.manager')->getStorage($entity_type->id()), |
| 64 | + $container->get('current_route_match'), |
| 65 | + $container->get('commerce_promotion.usage') |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public function load() { |
| 73 | + $promotion = $this->routeMatch->getParameter('commerce_promotion'); |
| 74 | + $coupons = $this->storage->loadMultipleByPromotion($promotion); |
| 75 | + // Load the usage counts for each coupon. |
| 76 | + $this->usageCounts = $this->usage->loadMultiple($coupons); |
| 77 | + |
| 78 | + return $coupons; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritdoc} |
| 83 | + */ |
| 84 | + public function buildHeader() { |
| 85 | + $header['code'] = $this->t('Code'); |
| 86 | + $header['usage'] = $this->t('Usage'); |
| 87 | + return $header + parent::buildHeader(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritdoc} |
| 92 | + */ |
| 93 | + public function buildRow(EntityInterface $entity) { |
| 94 | + /** @var \Drupal\commerce_promotion\Entity\CouponInterface $entity */ |
| 95 | + $current_usage = $this->usageCounts[$entity->id()]; |
| 96 | + $usage_limit = $entity->getUsageLimit(); |
| 97 | + $usage_limit = $usage_limit ?: $this->t('Unlimited'); |
| 98 | + $row['code'] = $entity->label(); |
| 99 | + if (!$entity->isEnabled()) { |
| 100 | + $row['code'] .= ' (' . $this->t('Disabled') . ')'; |
| 101 | + } |
| 102 | + $row['usage'] = $current_usage . ' / ' . $usage_limit; |
| 103 | + |
| 104 | + return $row + parent::buildRow($entity); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments