0

I am implementing a couple of translatable variables that are exposed via hook_variable_info().

Can I use placeholders in these variables, and process them to replace with the actual values?

As an example: I have a variable that holds the following informational text that will be shown on a page that has a registration form below.

Please signup here to gain access to the restricted area for @yourcompany

I have been googling around for this, but cannot seem to find any good leads on how to do this (if at all possible?)

5
  • Do you mean the variable is used to save the value for replacing the token @yourcompany?
    – Jimmy Ko
    Jul 29, 2016 at 22:22
  • Jimmy, the variable is used to store the entire message "please sign up...", INCLUDING the @yourcompany token
    – Guardian
    Jul 30, 2016 at 7:57
  • 1
    How about just use $message = variable_get_value('variable_name'); $message = format_string('@yourcompany' => $companyname)?
    – Jimmy Ko
    Jul 30, 2016 at 12:45
  • Hi Jimmy, I was using tokens right now. But this is a little more tedious, since i have to create the tokens in advance; Your approach is better since this will allow me to insert the placeholders dynamically! Feel free to add this as the answer so i can tag it.
    – Guardian
    Jul 30, 2016 at 13:57
  • I posted in answer. Cheers.
    – Jimmy Ko
    Jul 30, 2016 at 14:44

1 Answer 1

0

The simplest way is using format_string(), you can have 3 kind of replacement token for different sanitisation and formatting.

@variable: Escaped to HTML using check_plain(). Use this as the default choice for anything displayed on a page on the site.

%variable: Escaped to HTML and formatted using drupal_placeholder(), which makes it display as emphasized text.

!variable: Inserted as is, with no sanitization or formatting. Only use this for text that has already been prepared for HTML display (for example, user-supplied text that has already been run through check_plain() previously, or is expected to contain some limited HTML tags and has already been run through filter_xss() previously).

Example code:

$message = variable_get_value('variable_name');
$message = format_string($message, array('@yourcompany' => $company_name));

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.