ShipmentListBuilder.php in Commerce Shipping 8.2
File
src/ShipmentListBuilder.php
View source
<?php
namespace Drupal\commerce_shipping;
use CommerceGuys\Intl\Formatter\CurrencyFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ShipmentListBuilder extends EntityListBuilder {
protected $currencyFormatter;
protected $routeMatch;
protected $entitiesKey = 'shipments';
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrencyFormatterInterface $currency_formatter, RouteMatchInterface $route_match) {
parent::__construct($entity_type, $storage);
$this->currencyFormatter = $currency_formatter;
$this->routeMatch = $route_match;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('commerce_price.currency_formatter'), $container
->get('current_route_match'));
}
public function getFormId() {
return 'commerce_shipments';
}
protected function getEntityIds() {
$order_id = $this->routeMatch
->getRawParameter('commerce_order');
$query = $this
->getStorage()
->getQuery()
->accessCheck(FALSE)
->condition('order_id', $order_id)
->sort($this->entityType
->getKey('id'));
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$header = [
'label' => $this
->t('Shipment'),
'tracking' => $this
->t('Tracking'),
'amount' => $this
->t('Amount'),
'state' => $this
->t('State'),
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$amount = $entity
->getAmount();
$row['label']['data'] = [
'#type' => 'link',
'#title' => $entity
->label(),
] + $entity
->toUrl()
->toRenderArray();
$row['tracking'] = $entity
->getTrackingCode();
$row['amount'] = $this->currencyFormatter
->format($amount
->getNumber(), $amount
->getCurrencyCode());
$row['state'] = $entity
->getState()
->getLabel();
return $row + parent::buildRow($entity);
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->getOrder()
->access('resend_receipt')) {
$operations['resend_confirmation'] = [
'title' => $this
->t('Resend confirmation'),
'weight' => 20,
'url' => $entity
->toUrl('resend-confirmation-form'),
];
}
return $operations;
}
}