4

I've got a custom module that is sending some emails and I want to put the site name in the subject line, but whenever I look up the site_name in code, it is wrapped in html.

This is the code I use to generate the subject line:

$variables = array(
    '%site_name' => variable_get('site_name'),
    '%tags' => $params['tags'],
);

$subject = t('%site_name: Verify your Subscription', $variables);
error_log("subject=$subject");

In the email I received and in the error log, this is what $subject is:

<em class="placeholder">liberalgeek.com</em>: Verify your Subscription

Why is the site_name wrapped in html?

How can I either strip the html or lookup the site_name in a different way so that it doesn't have the html to start?

6

The problem is that you are using bad modifier for the t() function.

$variables = array(
    '@site_name' => variable_get('site_name'),
    '@tags' => $params['tags'],
);

$subject = t('@site_name: Verify your Subscription', $variables);
error_log("subject=$subject");

Note the @site_name change. If you use

  • @site_name -- it will be run through check_plain and returns without any theming.
  • %site_name -- check_plain with theme_placeholder.
  • !site_name -- Allows all HTML.

Also note that this is not limited to site name. Whenever you use t() function, you should use above placeholder pattern to ensure CSRF-safe variable display. Do not use t() function for use-entered variables. For an example, t($form_state['values']['name']); is bad!. Real use of t() function is when you use it with your own text and optionally user-entered data.

Good luck!

  • 1
    The site_name variable shouldn't be passed through t() either. Multilingual Variables (part of Internationalization) helps to specify and retrieve variables in different languages. Check out this blogpost for more info: hojtsy.hu/blog/2011-feb-25/… – balintk Jan 13 '13 at 22:40
  • thanks for pointing that! OP's use in t() with own text and sitename is correct though. – AyeshK Jan 14 '13 at 0:37
0

It is in the variable table. You can read it directly from the database. In Drupal 7 this value is of type blob (in Drupal 6 it was longtext). It is serialized so you cannot simply read it with a select query.

I have some PHP code here that shows how to write to it and read from it. http://www.siteconsortium.com/h/p1.php?id=drpl001

Also, if you are using Drupal 7 the link above it shows how read it in the UI (Configuration -> System -> Site Information). Modifying it here works.

Don't forgot to dump the cache though if cache is enabled if you change it in the database.

  • The linked page doesn't show code anymore. – kiamlaluno Mar 17 '18 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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