View source
<?php
namespace Drupal\payment\Entity\Payment;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentListBuilder extends EntityListBuilder implements PaymentListBuilderInterface {
protected $currencyStorage;
protected $dateFormatter;
protected $ownerId;
protected $redirectDestination;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$list_builder = parent::createInstance($container, $entity_type);
$list_builder->currencyStorage = $container
->get('entity_type.manager')
->getStorage('currency');
$list_builder->dateFormatter = $container
->get('date.formatter');
$list_builder->moduleHandler = $container
->get('module_handler');
$list_builder->redirectDestination = $container
->get('redirect.destination');
$list_builder->stringTranslation = $container
->get('string_translation');
return $list_builder;
}
public function setDateFormatter(DateFormatterInterface $date_formatter) {
$this->dateFormatter = $date_formatter;
}
public function setCurrencyStorage(EntityStorageInterface $currency_storage) {
$this->currencyStorage = $currency_storage;
}
public function restrictByOwnerId($owner_id) {
$this->ownerId = $owner_id;
return $this;
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery();
$header = $this
->buildHeader();
$query
->tableSort($header);
if ($this->ownerId) {
$query
->condition('owner', $this->ownerId);
}
return $query
->pager($this->limit)
->execute();
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('There are no payments yet.');
return $build;
}
public function buildHeader() {
$header['updated'] = [
'data' => $this
->t('Last updated'),
'field' => 'changed',
'sort' => 'DESC',
'specifier' => 'changed',
];
$header['status'] = [
'data' => $this
->t('Status'),
];
$header['amount'] = [
'data' => $this
->t('Amount'),
];
$header['payment_method'] = array(
'data' => $this
->t('Payment method'),
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
);
$header['owner'] = array(
'data' => $this
->t('Payer'),
'class' => array(
RESPONSIVE_PRIORITY_MEDIUM,
),
);
$header['operations'] = [
'data' => $this
->t('Operations'),
];
return $header;
}
public function buildRow(EntityInterface $payment) {
$row['data']['updated'] = $this->dateFormatter
->format($payment
->getChangedTime());
$status_definition = $payment
->getPaymentStatus()
->getPluginDefinition();
$row['data']['status'] = $status_definition['label'];
$currency = $this->currencyStorage
->load($payment
->getCurrencyCode());
if (!$currency) {
$currency = $this->currencyStorage
->load('XXX');
}
$row['data']['amount'] = $currency
->formatAmount($payment
->getAmount());
$row['data']['payment_method'] = $payment
->getPaymentMethod() ? $payment
->getPaymentMethod()
->getPluginDefinition()['label'] : $this
->t('Unavailable');
$row['data']['owner']['data'] = array(
'#theme' => 'username',
'#account' => $payment
->getOwner(),
);
$operations = $this
->buildOperations($payment);
$row['data']['operations']['data'] = $operations;
return $row;
}
protected function getDefaultOperations(EntityInterface $entity) {
$destination = $this->redirectDestination
->get();
$operations = parent::getDefaultOperations($entity);
foreach ($operations as &$operation) {
$operation['query']['destination'] = $destination;
}
if ($entity
->access('view')) {
$operations['view'] = array(
'title' => $this
->t('View'),
'weight' => -10,
'url' => $entity
->toUrl(),
);
}
if ($entity
->access('update_status')) {
$operations['update_status'] = array(
'title' => $this
->t('Update status'),
'attributes' => array(
'data-accepts' => 'application/vnd.drupal-modal',
),
'query' => array(
'destination' => $destination,
),
'url' => $entity
->toUrl('update-status-form'),
);
}
if ($entity
->access('capture')) {
$operations['capture'] = array(
'title' => $this
->t('Capture'),
'attributes' => array(
'data-accepts' => 'application/vnd.drupal-modal',
),
'query' => array(
'destination' => $destination,
),
'url' => $entity
->toUrl('capture-form'),
);
}
if ($entity
->access('refund')) {
$operations['refund'] = array(
'title' => $this
->t('Refund'),
'attributes' => array(
'data-accepts' => 'application/vnd.drupal-modal',
),
'query' => array(
'destination' => $destination,
),
'url' => $entity
->toUrl('refund-form'),
);
}
if ($entity
->access('complete')) {
$operations['complete'] = array(
'title' => $this
->t('Complete'),
'url' => $entity
->toUrl('complete'),
);
}
return $operations;
}
public function buildOperations(EntityInterface $entity) {
$build = parent::buildOperations($entity);
$build['#attached'] = array(
'library' => array(
'core/drupal.ajax',
),
);
return $build;
}
}