Skip to content

Commit

Permalink
Issue #2763255 by mglaman, bojanz: Implement a nicer discount listing
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Apr 21, 2017
1 parent 74ad0a4 commit a02d6d7
Showing 1 changed file with 73 additions and 21 deletions.
94 changes: 73 additions & 21 deletions modules/promotion/src/PromotionListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,6 +37,13 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface {
*/
protected $entities = [];

/**
* The usage counts.
*
* @var array
*/
protected $usageCounts;

/**
* Whether tabledrag is enabled.
*
Expand All @@ -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}
Expand All @@ -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;
}
Expand All @@ -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');
}
Expand All @@ -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',
Expand All @@ -97,7 +151,7 @@ public function buildRow(EntityInterface $entity) {
* {@inheritdoc}
*/
public function render() {
return \Drupal::formBuilder()->getForm($this);
return $this->formBuilder->getForm($this);
}

/**
Expand All @@ -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',
Expand All @@ -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']);
Expand Down

0 comments on commit a02d6d7

Please sign in to comment.