PriceListItemListBuilder.php in Commerce Pricelist 8.2
File
src/PriceListItemListBuilder.php
View source
<?php
namespace Drupal\commerce_pricelist;
use Drupal\commerce_price\Calculator;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PriceListItemListBuilder extends EntityListBuilder {
protected $entityTypeManager;
protected $routeMatch;
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
parent::__construct($entity_type, $entity_type_manager
->getStorage('commerce_pricelist_item'));
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'), $container
->get('current_route_match'));
}
protected function getEntityIds() {
$price_list = $this->routeMatch
->getParameter('commerce_pricelist');
$query = $this
->getStorage()
->getQuery()
->condition('price_list_id', $price_list
->id())
->sort('purchasable_entity')
->sort('quantity');
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$price_list = $this->routeMatch
->getParameter('commerce_pricelist');
$purchasable_entity_type = $this->entityTypeManager
->getDefinition($price_list
->bundle());
$header['purchasable_entity'] = $purchasable_entity_type
->getLabel();
$header['quantity'] = $this
->t('Quantity');
$header['list_price'] = $this
->t('List price');
$header['price'] = $this
->t('Price');
$header['status'] = $this
->t('Status');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['purchasable_entity'] = $entity
->getPurchasableEntity()
->getOrderItemTitle();
$row['quantity'] = Calculator::trim($entity
->getQuantity());
$row['list_price'] = [
'data' => [
'#type' => 'inline_template',
'#template' => '{{list_price|commerce_price_format|default("N/A")}}',
'#context' => [
'list_price' => $entity
->getListPrice(),
],
],
];
$row['price'] = [
'data' => [
'#type' => 'inline_template',
'#template' => '{{price|commerce_price_format}}',
'#context' => [
'price' => $entity
->getPrice(),
],
],
];
$row['status'] = $entity
->isEnabled() ? $this
->t('Enabled') : $this
->t('Disabled');
return $row + parent::buildRow($entity);
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('There are no prices yet.');
return $build;
}
}