Drupal - Passing variables that contain HTML through the t() function - ! placeholder has been removed
It appears that any arguments passed to t() function are escaped unless they implement MarkupInterface. So that you need to represent the name as an object.
use Drupal\Component\Render\FormattableMarkup;
$formatted_name = new FormattableMarkup(
'<span class="fullname-wrapper">
<span class="first-name">@first_name</span>
<span class="last-name">@second_name</span>
<span class="account-name-wrapper">(@user_name)</span>
</span>',
[
'@first_name' => 'John',
'@second_name' => 'Hancock',
'@user_name' => '@JohnH',
]
);
drupal_set_message(t('Your name is @name', ['@name' => $formatted_name]));
Yes, the user error you are seeing means the placeholder you are using is not recognized from t()
. FormattableMarkup::placeholderFormat()
, the method that does the job behind the scene, recognizes just 3 placeholder-types: @variable, %variable, and :variable. If the placeholder starts with a different character, it executes the following code, which causes the behavior you are seeing.
default:
// We do not trigger an error for placeholder that start with an
// alphabetic character.
// @todo https://www.drupal.org/node/2807743 Change to an exception
// and always throw regardless of the first character.
if (!ctype_alpha($key[0])) {
// We trigger an error as we may want to introduce new placeholders
// in the future without breaking backward compatibility.
trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_ERROR);
}
elseif (strpos($string, $key) !== FALSE) {
trigger_error('Invalid placeholder (' . $key . ') in string: ' . $string, E_USER_DEPRECATED);
}
// No replacement possible therefore we can discard the argument.
unset($args[$key]);
break;
The description for @variable says that the value of the variable could be:
- A string, passed through \Drupal\Component\Utility\Html::escape()
- A MarkupInterface object whose string value will not be sanitized
In your case, I would just use the following code.
use Drupal\Component\Render\FormattableMarkup;
drupal_set_message(
$this->t(
"@amount has been transferred to @account",
[
'@amount' => '¥' . $form_state->getValue('amount'),
'@account' => new FormattableMarkup($this->accountService->formatAccountName($account), [])
]
)
);