Skip to main content
Tweeted twitter.com/StackDrupal/status/801330074217750528
added 1147 characters in body
Source Link
Jaypan
  • 18k
  • 3
  • 34
  • 65

In Drupal 8, it appears that using the ! (exclamation mark) placeholder with the t() function has been removed.

I have a variable that contains HTML:

<span class="fullname_wrapper"><span class="first_name">John</span> <span class="last_name">Hancock</span> <span class="account_name_wrapper">(@JohnH)</span></span>

I want to keep this HTML, as it will be styled specifically wherever it appears on the site.

In Drupal 7, I was able to do this:

t('Your name is !name', array('!name' => $name));

In which $name contains the HTML shown above. However, in Drupal 8, this doesn't work since the exclamation mark pass-through placeholder has been removed.

I tried this:

t('Your name is :name', array(':name' => $name));

But the HTML is still escaped.

The @ mark also escapes the HTML.

Does anyone know how HTML can be passed through the t() function in D8?

UPDATE:

According to No Sssweat's comment, it seems that my the ! mark does seem to work in t(). So here is my actual code:

drupal_set_message(
  $this->t(
    "@amount has been transferred to !account",
    array(
      '@amount' => '¥' . $form_state->getValue('amount'),
      '!account' => $this->accountService->formatAccountName($account)
    )
  )
);

And I get this error stack:

User error: Invalid placeholder (!account) in string: @amount has been transferred to !account in Drupal\Component\Render\FormattableMarkup::placeholderFormat() (line 235 of core/lib/Drupal/Component/Render/FormattableMarkup.php).

Drupal\Component\Render\FormattableMarkup::placeholderFormat('@amount has been transferred to !user', Array) (Line: 204)
Drupal\Core\StringTranslation\TranslatableMarkup->render() (Line: 15)
Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (Line: 451)
drupal_set_message(Object) (Line: 128)

The user error is what made me think the ! mark is not allowed, but I must have misdiagnosed it. Does anyone know what is happening here?

In Drupal 8, it appears that using the ! (exclamation mark) placeholder with the t() function has been removed.

I have a variable that contains HTML:

<span class="fullname_wrapper"><span class="first_name">John</span> <span class="last_name">Hancock</span> <span class="account_name_wrapper">(@JohnH)</span></span>

I want to keep this HTML, as it will be styled specifically wherever it appears on the site.

In Drupal 7, I was able to do this:

t('Your name is !name', array('!name' => $name));

In which $name contains the HTML shown above. However, in Drupal 8, this doesn't work since the exclamation mark pass-through placeholder has been removed.

I tried this:

t('Your name is :name', array(':name' => $name));

But the HTML is still escaped.

The @ mark also escapes the HTML.

Does anyone know how HTML can be passed through the t() function in D8?

In Drupal 8, it appears that using the ! (exclamation mark) placeholder with the t() function has been removed.

I have a variable that contains HTML:

<span class="fullname_wrapper"><span class="first_name">John</span> <span class="last_name">Hancock</span> <span class="account_name_wrapper">(@JohnH)</span></span>

I want to keep this HTML, as it will be styled specifically wherever it appears on the site.

In Drupal 7, I was able to do this:

t('Your name is !name', array('!name' => $name));

In which $name contains the HTML shown above. However, in Drupal 8, this doesn't work since the exclamation mark pass-through placeholder has been removed.

I tried this:

t('Your name is :name', array(':name' => $name));

But the HTML is still escaped.

The @ mark also escapes the HTML.

Does anyone know how HTML can be passed through the t() function in D8?

UPDATE:

According to No Sssweat's comment, it seems that my the ! mark does seem to work in t(). So here is my actual code:

drupal_set_message(
  $this->t(
    "@amount has been transferred to !account",
    array(
      '@amount' => '¥' . $form_state->getValue('amount'),
      '!account' => $this->accountService->formatAccountName($account)
    )
  )
);

And I get this error stack:

User error: Invalid placeholder (!account) in string: @amount has been transferred to !account in Drupal\Component\Render\FormattableMarkup::placeholderFormat() (line 235 of core/lib/Drupal/Component/Render/FormattableMarkup.php).

Drupal\Component\Render\FormattableMarkup::placeholderFormat('@amount has been transferred to !user', Array) (Line: 204)
Drupal\Core\StringTranslation\TranslatableMarkup->render() (Line: 15)
Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (Line: 451)
drupal_set_message(Object) (Line: 128)

The user error is what made me think the ! mark is not allowed, but I must have misdiagnosed it. Does anyone know what is happening here?

Source Link
Jaypan
  • 18k
  • 3
  • 34
  • 65

Passing variables that contain HTML through the t() function - ! placeholder has been removed

In Drupal 8, it appears that using the ! (exclamation mark) placeholder with the t() function has been removed.

I have a variable that contains HTML:

<span class="fullname_wrapper"><span class="first_name">John</span> <span class="last_name">Hancock</span> <span class="account_name_wrapper">(@JohnH)</span></span>

I want to keep this HTML, as it will be styled specifically wherever it appears on the site.

In Drupal 7, I was able to do this:

t('Your name is !name', array('!name' => $name));

In which $name contains the HTML shown above. However, in Drupal 8, this doesn't work since the exclamation mark pass-through placeholder has been removed.

I tried this:

t('Your name is :name', array(':name' => $name));

But the HTML is still escaped.

The @ mark also escapes the HTML.

Does anyone know how HTML can be passed through the t() function in D8?