1

For design purposes I need line breaks in a multilingual site name. I have managed to do so by adding <br /> tags in the site name variable. But they show in the title within the html head tag and I don't know how to get rid of them.

I am using a custom Zen subtheme to which I have added the html.tpl.php file. In that I have tried the following approaches to no avail:

<title><?php print preg_replace('/<br \/>/iU', '', $head_title); ?></title>

<title><?php print strip_tags($head_title); ?></title>

<title><?php $head_title = str_replace('<br />', '', $head_title);
  print $head_title; ?></title>

Please excuse any profanities I may have committed in the use of php, I am no coder. Any ideas are most welcome.

5
  • Why not just use CSS to set a width on the <h1> element that forces the line break naturally? It would probably be easier :) Oh, and did you clear the caches after adding the template file? It won't be picked up until you do. All 3 of the code examples you've given should work perfectly
    – Clive
    Commented Dec 6, 2012 at 18:16
  • That was my first option exactly. But since the site is multilingual it is not possible to find a with that works for every language.
    – castaway
    Commented Dec 6, 2012 at 18:19
  • Sorry I didn't twig, that makes sense
    – Clive
    Commented Dec 6, 2012 at 18:19
  • 1
    Are you sure you're modifying the correct template file ? I mean if you put <title>test</title> and clear your cache, will your page display test as a title ?
    – tostinni
    Commented Dec 6, 2012 at 20:23
  • Yes and yes. Still a good point though.
    – castaway
    Commented Dec 11, 2012 at 16:43

2 Answers 2

1

Here is what I just found: There is no tags in the title, it's just a string. So the fix is quite simple.

in the html.tpl.php replace:

<title><?php print $head_title ?></title>

With this:

<title><?php 
$break_tag = array("&lt;br /&gt;");
$simple_space = array(" ");
$new_head_title = str_replace($break_tag, $simple_space, $head_title);
print $new_head_title;
?></title>

Hope it helps.

2
  • Your code seems much neater. Did you check how the site name works in emails from your site after that?
    – castaway
    Commented Sep 14, 2013 at 10:41
  • This will affect only the title tag on the page, so emails, reports, etc will still have the ugly br tag in the site name. Maybe the site name could be preprocessed?
    – chonumb
    Commented Sep 23, 2013 at 14:10
0

I think I have found a way, albeit it might not be a very orthodox one, but just in case anybody else is facing a similar situation, here it is:

In your theme's html.tpl.ph, edit the title tag into something like this:

<title><?php print strip_tags($head_title); ?></title>

Then, in your theme's template.php, add this:

function YOUR-THEME-NAME_preprocess_html(&$vars) {
      global $theme_path;
      // Add conditional CSS for IE7 and below.
      drupal_add_css($theme_path . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
      $vars['head_title'] = implode(' | ', array(drupal_get_title(), variable_get('site_name') . ' ' . variable_get('site_slogan')));  
    }

In my case I ended up removing the <br /> tags from the site name since they were showing all over, not just on the page title but also on emails, reports, etc. What I did instead was leave the first line as the site name and put the two following in the site slogan, with a <br /> line break. Then I used some css and the previous theme function so that everything looks as it should.

1
  • Anyone could suggest a solution for this but in drupal 8?
    – cwiggo
    Commented Apr 14, 2017 at 22:21

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.