PaymentOption::getLabel() fails to build label if missing card_number

Created on 3 July 2025, 3 days ago

Describe your bug or feature request.

TypeError: Drupal\commerce_payment\PaymentOption::getLabel(): Return value must be of type string, Drupal\Core\StringTranslation\TranslatableMarkup returned in Drupal\commerce_payment\PaymentOption->getLabel() (line 88 of modules/contrib/commerce/modules/payment/src/PaymentOption.php).

The code in question is:

    $card_number = $payment_method->card_number->value;
    $args = [
      '@card_type' => $card_type,
      '@card_number' => $card_number,
    ];
    return $card_number ? $this->t('@card_type ending in @card_number', $args) : $this->t('@card_type', $args);

If the $card_number is null it fails because t() expects arguments even though @card_number is not being rendered.

Trying to convert to string returns:
TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given, called in /app/web/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 238 in Drupal\Component\Utility\Html::escape() (line 431 of core/lib/Drupal/Component/Utility/Html.php).

🐛 Bug report
Status

Active

Version

3.0

Component

Payment

Created by

🇧🇷Brazil joao sausen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024