2

I want to use the token [user:name] in my module. This module contains a sample ControllerBase class

In my class I test this:

class Page_test_Controller extends ControllerBase {

    /**
     * {@inheritdoc}
     */
    public function token() {
        $build = array(
            '#type' => 'markup',
            '#markup' => t('[user:name] hello'),
        );
        return $build;
    }

}

When I load my page the token isn't replaced as shown here:

enter image description here

What am I missing or doing wrong?

1 Answer 1

4

Translatable strings don't use tokens, but placeholders, see for example https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Render%21FormattableMarkup.php/function/FormattableMarkup%3A%3AplaceholderFormat/8 for some documentation, or https://www.drupal.org/node/322729 (more 7.x, but conceptually still mostly OK).

Tokens are when you have user configurable text like the user register/confirmation mails. It doesn't make sense to have token in a text that is hardcoded since tokens are only useful if that text can be customized by the user. For documentation on how to use the token API, see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21Token.php/class/Token/8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.