Since a short time descriptions in GA module are checkplained.

This is correct code:

    $site_search_dependencies = '<div class="admin-requirements">';
    $site_search_dependencies .= t('Requires: !module-list', array('!module-list' => (\Drupal::moduleHandler()->moduleExists('search') ? t('@module (<span class="admin-enabled">enabled</span>)', array('@module' => 'Search')) : t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => 'Search')))));
    $site_search_dependencies .= '</div>';

    $form['tracking']['search_and_advertising']['google_analytics_site_search'] = array(
      '#type' => 'checkbox',
      '#title' => t('Track internal search'),
      '#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <strong>search</strong>. For more information see <a href="@url">Setting Up Site Search for a Profile</a>.', array('@url' => 'https://support.google.com/analytics/answer/1012264')) . $site_search_dependencies,
      '#default_value' => $config->get('track.site_search'),
      '#disabled' => (\Drupal::moduleHandler()->moduleExists('search') ? FALSE : TRUE),
    );

Why does this happen and how should this fixed?

Comments

fabianx’s picture

Category: Bug report » Support request

This does happen, because you concat a safe string - coming from t() with an unsafe one.

In this particular case I think you know that the string is safe, so you can do:

t('...!dependencies', array('!dependencies' => $site_search_dependencies, ...));
hass’s picture

Nope. This is against translation string rules. That is technically possible, but forbidden.

xjm’s picture

hass’s picture

I'm asking me how I could implement a template inside a #description form field... that is not documented.

hass’s picture

Also tried SafeMarkup::set($site_search_dependencies) in the form, but this is not working. Also tried setting it on the strings, but it can only be wrong to set this on a t()'ified string. not working here too. I still see <span class="admin-enabled">enabled</span> in config pages.

mpdonadio’s picture

You can use an inline template in the #description

$description = array(
  '#type' => 'inline_template',
  '#template' => '<strong>This an {{ foo }} inline template</strong>',
  '#context' => array('foo' => t('awesome')),
);

...

$form['tracking']['search_and_advertising']['google_analytics_site_search'] = array(
...
'#description' => $description,
...
);

Normal usage for Twig applies.

fabianx’s picture

We are working on a generic fix in core.

I am sorry to leave you waiting, but this should be working soon:

#2324371: Fix common HTML escaped render #key values due to Twig autoescape

hass’s picture

Status: Active » Fixed

@Fabianx: Thanks for the link. Marking fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.