Hi There

Does any one know how to create a line break in a Drupal title? To force the title over two lines.

I've tried
with no joy. Is it possible?

Comments

OpenChimp’s picture

I'd also love to know if there is a method for doing this. Many of the articles I publish are multi-part, and it would be great to have a way to break them up.

mmaul’s picture

I’m trying to do the same at the moment. From a semantic point of view, this is problematic IMO because adding line break information to text rendered inside a header tag would mean adding display markup at the wrong place.

Anyway, one of my customers is asking for it, and he probably won’t buy the semantic argument. ;-) So my first and admittedly hacky route would be to add some unobtrusive character (maybe ‘•’ or ‘·’ ) in the title text at the place where the line break is intended and convert this to a <br/> when the title is rendered. Putting a <br/> in the title text will produce strange results for those using RSS readers, so an inconspicuous, yet easily insertable character would probably be best suited.

I’m going to implement this in one of my local modules and report back here, but I guess you shouldn’t expect this to become mainstream, as one shouldn’t insert display markup where it doesn’t belong. Additionally, a forced layout will break when users change display text size.

Cheers,
Mathias

KenleyT’s picture

Hey,

mmaul, that's a lot of work. ;-) You can just go to MS Word's symbol table and "Special Characters" and use those blank em spaces there. The blank spaces is treated differently from the space bar's space and can push your titles to the next line. Hope this helps.

Thanks,
Ken

stackpool’s picture

I tried this. It seemed to work in the preview, but then it turned into a question mark when I saved, and then I got a fatal error in includes/common.inc (Unsupported operand types) and now can't get back to the page to edit out the em space.

So, maybe back to the drawing board with this one...

Thanks anyway!

mmaul’s picture

Ken,

this wouldn’t have worked with this specific client. He couldn’t even find the “|” character, so he wouldn’t have been able to find, insert and remember any special whitecase characters …

Mathias

mmaul’s picture

Eerie … two years later, I find my own post after doing a Google search for the same issue. ;-)

(BTW, I *did* manage to convince the client, so I didn’t implement the proposed solution.)

WorldFallz’s picture

I solved this by adding a subtitle field and some theming in node.tpl.php and page.tpl.php.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

wnwek’s picture

I used CSS to break up my title over two parts. I used the "width" tag and made it progressively smaller till the title broke over two parts.

chrisfargen’s picture

I was just about to use this approach as well. If you use it, you have to be prepared for lines to break unpredictably, depending on the length of the words in your content. For a small amount of content, I found it most useful to specify breaks manually. I used the approach from this link: http://drupal.org/node/28537

keva’s picture

I used the instructions here: http://drupal.org/node/28537

but added a break also, so my template.php looks like this:


/* Inserting HTML into node titles
 * http://drupal.org/node/28537
 */

function bb2html($text) {
  $bbcode = array(
                  "[strong]", "[/strong]",
                  "[b]",  "[/b]",
                  "[i]",  "[/i]",
                  "[br]",
                  "[em]", "[/em]"
                );
  $htmlcode = array(
                "<strong>", "</strong>",
                "<strong>", "</strong>",
                "<em>", "</em>",
                "<br />",
                "<em>", "</em>"
              );
  return str_replace($bbcode, $htmlcode, $text);
}

function bb_strip($text) {
  $bbcode = array(
                  "[strong]", "[/strong]",
                  "[b]",  "[/b]",
                  "[i]",  "[/i]",
                  "[br]",
                  "[em]", "[/em]"
                );
  return str_replace($bbcode, '', $text);
}
Vincent Verheyen’s picture

@keva - This is not working for me. The php doesn't seem to be loaded correctly. When trying to load a node, the page displays some lines of the .php file, visible to the end-user; and further more, e.g. the breaks aren't formatted.

appaulmac’s picture

I only wanted to handle line breaks. I make use of the 'pipe' character to delimit a line break:

This title is split|Now on the second line

Copy block.tpl.php from /modules/block to your
/templates folder,

Look for the line where the block title is output:
<?php print $block->subject ?>

Change it to
<?php print str_replace('|','<br />',$block->subject) ?>

Remember to clear your theme cache so the template file will be recognised!

ismail cherri’s picture

I love this solution. Many thanks :)

Katy Jockelson’s picture

Thank you, this was a great solution for me. After using a similar thing in a block template (to control the output of the Delta block for page title), I used this code in html.tpl.php to control the output of the html (head) title, so the pipe character didn't show in the browser tab.

stolzenhain’s picture

This is the solution I used as well – it's only now that I see that this opens a can of worms applied to the node title: You need to fix the HTML head, views using titles etc. as well or all your outputs are cluttered with pipe symbols.

Is there a way to invoke this via some kind of hook so it happens before additional processing?

I recall handling this via a special linebreak character once but couldn't reproduce this behaviour on recent installations.

stolzenhain’s picture

Ok, so regarding the can-of-worms-aspect of starting to fix up your title output everywhere on the site (RSS feeds, content lists, search results …) I recommend against messing with the title field and considering the following approach:

  1. Create an alternative title field – depending on your UX strategy, this can be a real multiline field or a text field you plan to split up on a special character
  2. Use Automatic Nodetitles to hide your actual title field and
  3. apply a PHP function to copy the contents of your alternative title minus linebreaks / special character in your hidden title field (comes with the module).
  4. now replace the title output with your alternative field everywhere you want your multiline field to appear: be it views or node templates.

Whenever the system is using the internal title display – on searches, RSS feeds etc, the version without special characters / linebreaks will be available without additional hotfixing from your side.

The only downside of this approach is, that it takes a bit of fiddling to apply it on an already running site.

Vincent Verheyen’s picture

@appaulmac - Would you have any idea as to why this is not working for me? I specifically find all the files and folders you mentioned, and adjusted the lines and copied the files in to the correct folder of my Bootstrap sub-theme. Even after clearing all caches, the "|" doesn't brake the lines ...

Gralisto’s picture

- controling line breaks in block title -
this replacement : print str_replace('|','
',$block->subject)
works right, and more than this tells how to modify whatever you want, from default template
to specific template,
Thanks very much...

paulabg’s picture

To create a line break you need to use the constant PASS_THROUGH

Example

drupal_set_title('text on line 1
text on line 2', PASS_THROUGH);

Reference: https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drup...