class PaymentListBuilder in Commerce Core 8.2
Defines the list builder for payments.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
- class \Drupal\commerce_payment\PaymentListBuilder
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
Expanded class hierarchy of PaymentListBuilder
File
- modules/
payment/ src/ PaymentListBuilder.php, line 18
Namespace
Drupal\commerce_paymentView source
class PaymentListBuilder extends EntityListBuilder {
/**
* The currency formatter.
*
* @var \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface
*/
protected $currencyFormatter;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* {@inheritdoc}
*/
protected $entitiesKey = 'payments';
/**
* {@inheritdoc}
*
* Set limit to false so the list is not paginated.
*/
protected $limit = FALSE;
/**
* The order.
*/
protected $order;
/**
* Constructs a new PaymentListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter
* The currency formatter.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrencyFormatterInterface $currency_formatter, RouteMatchInterface $route_match, DateFormatterInterface $date_formatter) {
parent::__construct($entity_type, $storage);
$this->currencyFormatter = $currency_formatter;
$this->routeMatch = $route_match;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
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'), $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'commerce_payments';
}
/**
* {@inheritdoc}
*/
public function load() {
$this->order = $this->routeMatch
->getParameter('commerce_order');
return $this->storage
->loadMultipleByOrder($this->order);
}
/**
* {@inheritdoc}
*/
protected function getDefaultOperations(EntityInterface $entity) {
$operations = [];
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $entity */
$payment_gateway = $entity
->getPaymentGateway();
// Add the gateway-specific operations.
if ($payment_gateway) {
$operations = $payment_gateway
->getPlugin()
->buildPaymentOperations($entity);
// Filter out operations that aren't allowed.
$operations = array_filter($operations, function ($operation) {
return !empty($operation['access']);
});
// Build the url for each operation.
$base_route_parameters = [
'commerce_payment' => $entity
->id(),
'commerce_order' => $entity
->getOrderId(),
];
foreach ($operations as $operation_id => $operation) {
$route_parameters = $base_route_parameters + [
'operation' => $operation_id,
];
$operation['url'] = new Url('entity.commerce_payment.operation_form', $route_parameters);
$operations[$operation_id] = $operation;
}
}
// Add the non-gateway-specific operations.
if ($entity
->access('delete')) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'url' => $entity
->toUrl('delete-form'),
];
}
return $operations;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this
->t('Payment');
$header['state'] = $this
->t('State');
$header['payment_gateway'] = $this
->t('Payment gateway');
$header['authorized'] = $this
->t('Authorized');
$header['completed'] = $this
->t('Completed');
$header['avs_response'] = '';
$header['remote_id'] = $this
->t('Remote ID');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $entity */
$amount = $entity
->getAmount();
$formatted_amount = $this->currencyFormatter
->format($amount
->getNumber(), $amount
->getCurrencyCode());
$refunded_amount = $entity
->getRefundedAmount();
if ($refunded_amount && !$refunded_amount
->isZero()) {
$formatted_amount .= ' ' . $this
->t('Refunded:') . ' ';
$formatted_amount .= $this->currencyFormatter
->format($refunded_amount
->getNumber(), $refunded_amount
->getCurrencyCode());
}
$payment_gateway = $entity
->getPaymentGateway();
$row['label'] = $formatted_amount;
$row['state'] = $entity
->getState()
->getLabel();
$row['payment_gateway'] = $payment_gateway ? $payment_gateway
->label() : $this
->t('N/A');
foreach ([
'authorized',
'completed',
] as $field) {
if ($entity
->get($field)
->isEmpty()) {
$row[$field] = '';
continue;
}
$row[$field] = $this->dateFormatter
->format($entity
->get($field)->value, 'short');
}
// Add the AVS response code label beneath the gateway name if it exists.
if ($avs_response_code = $entity
->getAvsResponseCode()) {
$row['avs_response'] = $this
->t('AVS response: [@code] @label', [
'@code' => $avs_response_code,
'@label' => $entity
->getAvsResponseCodeLabel(),
]);
}
else {
$row['avs_response'] = '';
}
$row['remote_id'] = $entity
->getRemoteId() ?: $this
->t('N/A');
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*
* Make the payment list page themeable. Add total_paid and order_balance
* elements.
*/
public function render() {
$build = parent::render();
$build['payment_total_summary'] = [
'#theme' => 'commerce_payment_total_summary',
'#order_entity' => $this->order,
];
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The entity storage class. | 1 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
protected | function | Ensures that a destination is present on the given URL. | |
EntityListBuilder:: |
protected | function | Loads entity IDs using a pager sorted by the entity id. | 4 |
EntityListBuilder:: |
protected | function | Gets the label of an entity. | |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentListBuilder:: |
protected | property | The currency formatter. | |
PaymentListBuilder:: |
protected | property | The date formatter. | |
PaymentListBuilder:: |
protected | property | ||
PaymentListBuilder:: |
protected | property |
Set limit to false so the list is not paginated. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
protected | property | The order. | |
PaymentListBuilder:: |
protected | property | The current route match. | |
PaymentListBuilder:: |
public | function |
Builds the header row for the entity listing. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
public | function |
Builds a row for an entity in the entity listing. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
protected | function |
Gets this list's default operations. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
public | function | ||
PaymentListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
public | function |
Make the payment list page themeable. Add total_paid and order_balance
elements. Overrides EntityListBuilder:: |
|
PaymentListBuilder:: |
public | function |
Constructs a new PaymentListBuilder object. Overrides EntityListBuilder:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |