From a02d6d7f5cc1cdaf2eb665ad33b1261c01ce96f0 Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Sat, 22 Apr 2017 00:10:06 +0200 Subject: [PATCH] Issue #2763255 by mglaman, bojanz: Implement a nicer discount listing --- .../promotion/src/PromotionListBuilder.php | 94 ++++++++++++++----- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/modules/promotion/src/PromotionListBuilder.php b/modules/promotion/src/PromotionListBuilder.php index 4d2c0c3faa..45dd2e91ce 100644 --- a/modules/promotion/src/PromotionListBuilder.php +++ b/modules/promotion/src/PromotionListBuilder.php @@ -4,20 +4,31 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityListBuilder; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Defines the list builder for shipping methods. + * Defines the list builder for promotions. */ class PromotionListBuilder extends EntityListBuilder implements FormInterface { /** - * The key to use for the form element containing the entities. + * The form builder. + * + * @var \Drupal\Core\Form\FormBuilderInterface + */ + protected $formBuilder; + + /** + * The usage. * - * @var string + * @var \Drupal\commerce_promotion\PromotionUsageInterface */ - protected $entitiesKey = 'promotions'; + protected $usage; /** * The entities being listed. @@ -26,6 +37,13 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface { */ protected $entities = []; + /** + * The usage counts. + * + * @var array + */ + protected $usageCounts; + /** * Whether tabledrag is enabled. * @@ -34,11 +52,35 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface { protected $hasTableDrag = TRUE; /** - * The form builder. + * Constructs a new PromotionListBuilder object. * - * @var \Drupal\Core\Form\FormBuilderInterface + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type definition. + * @param \Drupal\Core\Entity\EntityStorageInterface $storage + * The entity storage. + * @param \Drupal\Core\Form\FormBuilderInterface $form_builder + * The form builder. + * @param \Drupal\commerce_promotion\PromotionUsageInterface $usage + * The usage. */ - protected $formBuilder; + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, FormBuilderInterface $form_builder, PromotionUsageInterface $usage) { + parent::__construct($entity_type, $storage); + + $this->formBuilder = $form_builder; + $this->usage = $usage; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + return new static( + $entity_type, + $container->get('entity.manager')->getStorage($entity_type->id()), + $container->get('form_builder'), + $container->get('commerce_promotion.usage') + ); + } /** * {@inheritdoc} @@ -55,6 +97,8 @@ public function load() { $entities = $this->storage->loadMultiple($entity_ids); // Sort the entities using the entity class's sort() method. uasort($entities, [$this->entityType->getClass(), 'sort']); + // Load the usage counts for each promotion. + $this->usageCounts = $this->usage->getUsageMultiple($entities); return $entities; } @@ -64,7 +108,9 @@ public function load() { */ public function buildHeader() { $header['name'] = $this->t('Name'); - $header['status'] = $this->t('Enabled'); + $header['usage'] = $this->t('Usage'); + $header['start_date'] = $this->t('Start date'); + $header['end_date'] = $this->t('End date'); if ($this->hasTableDrag) { $header['weight'] = $this->t('Weight'); } @@ -75,11 +121,19 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - /** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $entity */ + $current_usage = $this->usageCounts[$entity->id()]; + $usage_limit = $entity->getUsageLimit(); + $usage_limit = $usage_limit ?: $this->t('Unlimited'); + /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $entity */ $row['#attributes']['class'][] = 'draggable'; $row['#weight'] = $entity->getWeight(); $row['name'] = $entity->label(); - $row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled'); + if (!$entity->isEnabled()) { + $row['name'] .= ' (' . $this->t('Disabled') . ')'; + } + $row['usage'] = $current_usage . ' / ' . $usage_limit; + $row['start_date'] = $entity->getStartDate()->format('M jS Y'); + $row['end_date'] = $entity->getEndDate() ? $entity->getEndDate()->format('M jS Y') : '—'; if ($this->hasTableDrag) { $row['weight'] = [ '#type' => 'weight', @@ -97,7 +151,7 @@ public function buildRow(EntityInterface $entity) { * {@inheritdoc} */ public function render() { - return \Drupal::formBuilder()->getForm($this); + return $this->formBuilder->getForm($this); } /** @@ -115,27 +169,25 @@ public function buildForm(array $form, FormStateInterface $form_state) { $delta = ceil($count / 2); } - $form[$this->entitiesKey] = [ + $form['promotions'] = [ '#type' => 'table', '#header' => $this->buildHeader(), '#empty' => $this->t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]), ]; foreach ($this->entities as $entity) { $row = $this->buildRow($entity); - if (isset($row['name'])) { - $row['name'] = ['#markup' => $row['name']]; - } - if (isset($row['status'])) { - $row['status'] = ['#markup' => $row['status']]; - } + $row['name'] = ['#markup' => $row['name']]; + $row['usage'] = ['#markup' => $row['usage']]; + $row['start_date'] = ['#markup' => $row['start_date']]; + $row['end_date'] = ['#markup' => $row['end_date']]; if (isset($row['weight'])) { $row['weight']['#delta'] = $delta; } - $form[$this->entitiesKey][$entity->id()] = $row; + $form['promotions'][$entity->id()] = $row; } if ($this->hasTableDrag) { - $form[$this->entitiesKey]['#tabledrag'][] = [ + $form['promotions']['#tabledrag'][] = [ 'action' => 'order', 'relationship' => 'sibling', 'group' => 'weight', @@ -162,7 +214,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - foreach ($form_state->getValue($this->entitiesKey) as $id => $value) { + foreach ($form_state->getValue('promotions') as $id => $value) { if (isset($this->entities[$id]) && $this->entities[$id]->getWeight() != $value['weight']) { // Save entity only when its weight was changed. $this->entities[$id]->setWeight($value['weight']);