View source
<?php
namespace Drupal\commerce_invoice;
use Drupal\commerce_invoice\Entity\InvoiceType;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InvoiceListBuilder extends EntityListBuilder {
protected $dateFormatter;
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, DateFormatter $date_formatter) {
parent::__construct($entity_type, $entity_type_manager
->getStorage($entity_type
->id()));
$this->dateFormatter = $date_formatter;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'), $container
->get('date.formatter'));
}
public function buildHeader() {
$header = [
'invoice_number' => [
'data' => $this
->t('Invoice number'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'date' => [
'data' => $this
->t('Date'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'type' => [
'data' => $this
->t('Type'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'customer' => [
'data' => $this
->t('Customer'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'state' => [
'data' => $this
->t('State'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$invoice_type = InvoiceType::load($entity
->bundle());
$row = [
'invoice_number' => $entity
->label(),
'date' => $this->dateFormatter
->format($entity
->getInvoiceDateTime(), 'short'),
'type' => $invoice_type
->label(),
'customer' => [
'data' => [
'#theme' => 'username',
'#account' => $entity
->getCustomer(),
],
],
'state' => $entity
->getState()
->getLabel(),
];
return $row + parent::buildRow($entity);
}
protected function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity
->access('view')) {
$options = [
'language' => $entity
->language(),
];
$operations['view'] = [
'title' => t('View'),
'url' => $entity
->toUrl('canonical', $options),
'weight' => -50,
];
$operations['download'] = [
'title' => t('Download'),
'url' => $entity
->toUrl('download', $options),
];
}
if ($entity
->access('update') && !$entity
->isPaid()) {
$operations['pay'] = [
'title' => t('Pay'),
'url' => $entity
->toUrl('payment-form'),
'weight' => 50,
];
}
return $operations;
}
}