Score:1

How can I provide my own replacement patterns to be used in config form?

pe flag

I want to give a configuration form where user can configuration text on a widget, like the description and button label, however I want to provide a replacement pattern to be used, like {{count}} so the user can write for instance "{{ count }} images remaining". How can I provide replacement patterns to be used in the configuration form?

Score:1
cn flag

{{ count }} is a Twig variable pattern. Parse the content before you display it as Twig inline template, to replace the variable dynamically:

use Drupal\Component\Utility\Xss;

  $build = [
    '#type' => 'inline_template',
    '#template' => $text,
    '#context' => [
      'count' => $dynamic_count_value,
    ],
    '#post_render' => [
      function ($children, $elements) {
        return Xss::filterAdmin($children);
      },
    ],
  ];

Xss:filterAdmin() is only safe for admin forms. So don't make the config form accessible to other users.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.