6

I am trying to change the site name when a user enters specific pages on my website. I know the variable for the site name is $site_name and is being retrieved from the system.tpl.html file.

So I am trying to set different site names depending the URL; for example, when a user visits /about_us, the site name should change.

In my template.php file, I have captured the path in a variable.

$path = drupal_get_path_alias();
if ($path == 'about_us') {
  $site_name = 'New title';
}

This won't work.

How can this be achieved?

2 Answers 2

9

I think you mean page.tpl.php (not system.tpl.html, which isn't a standard Drupal file).

That said, you need to put your code in a hook_preprocess_page() function inside template.php, and actually alter the template variables that are passed in. At the moment you're just creating a variable called $site_name in the local scope, and Drupal has no way to pick that up.

Try this:

function MYTHEME_preprocess_page(&$vars) {
  $path = drupal_get_path_alias();
  if ($path == 'about_us') {
    $vars['site_name'] = 'New title';
  }
}

Have a look at Setting up variables for use in a template for a bit more info.

2
  • yes:) thank you for your quick answer. It worked. Millions of thanks:):)
    – paranod
    Jul 4, 2012 at 13:26
  • @Nicole No probs, glad you got it sorted
    – Clive
    Jul 4, 2012 at 13:29
4

The site name is set in template_preprocess_page() using the following code.

  $variables['site_name'] = (theme_get_setting('toggle_name') ? filter_xss_admin(variable_get('site_name', 'Drupal')) : '');

In your theme, or module, you should use code similar to the following one. (Replace mycode with the short name of your module/theme.)

function mycode_preprocess_page(&$variables) {
  if (theme_get_setting('toggle_name') && drupal_get_path_alias() == 'about_us') {
    $variables['site_name'] = filter_xss_admin('The new site name');
  }
}

The call to filter_xss_admin() is not necessary, if you are not using the input obtained from a user.
You should always check the value returned by theme_get_setting('toggle_name'), as the site name could be toggled off for the theme currently used.

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.