You are here

public function BalanceTransactor::getTransactionDescription in Transaction 8

Compose a human readable description for the given transaction.

Parameters

\Drupal\transaction\TransactionInterface $transaction: The transaction to describe.

string $langcode: (optional) For which language the transaction description should be composed, defaults to the current content language.

Return value

string|\Drupal\Core\StringTranslation\TranslatableMarkup A string or translatable markup with the generated description.

Overrides TransactorBase::getTransactionDescription

File

src/Plugin/Transaction/BalanceTransactor.php, line 94

Class

BalanceTransactor
Transactor for accounting type transactions.

Namespace

Drupal\transaction\Plugin\Transaction

Code

public function getTransactionDescription(TransactionInterface $transaction, $langcode = NULL) {
  if ($transaction
    ->isNew()) {
    return parent::getTransactionDescription($transaction, $langcode);
  }
  $settings = $transaction
    ->getType()
    ->getPluginSettings();

  // Transaction amount.
  $amount = $transaction
    ->get($settings['amount'])->value;
  $t_options = $langcode ? [
    'langcode' => $langcode,
  ] : [];
  $t_args = [
    '@status' => $transaction
      ->isPending() ? $this
      ->t('(pending)') : '',
  ];
  if ($amount > 0) {
    $description = $this
      ->t('Credit transaction @status', $t_args, $t_options);
  }
  elseif ($amount < 0) {
    $description = $this
      ->t('Debit transaction @status', $t_args, $t_options);
  }
  else {
    $description = $this
      ->t('Zero amount transaction @status', $t_args, $t_options);
  }
  return $description;
}