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.

  • 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 Dec 6 '12 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 Dec 6 '12 at 18:19
  • Sorry I didn't twig, that makes sense – Clive Dec 6 '12 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 Dec 6 '12 at 20:23
  • Yes and yes. Still a good point though. – castaway Dec 11 '12 at 16:43
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.

| improve this answer | |
  • Your code seems much neater. Did you check how the site name works in emails from your site after that? – castaway Sep 14 '13 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 Sep 23 '13 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.

| improve this answer | |
  • Anyone could suggest a solution for this but in drupal 8? – cwiggo Apr 14 '17 at 22:21

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.