Is there any way of truncating a title over a certain number of characters (basically so it fits in my breadcrumb area)? I think this option was available in Drupal's breadcrumbs!?

The result is something like this

Home > Blog > This title is too long to...

Many this is a feature request?

Thanks
S

Comments

Jennifer_M’s picture

FWIW (as this is an oldish thread & you've probably solved your problem some other way) I was just thinking about this too.

As far as I'm aware, this doesn't exist within Custom Breadcrumbs (yet), but I think it's doable from within a theme_breadcrumb function.

After

$title = drupal_get_title();

you could use PHP to establish the title's length, and if appropriate truncate it and add the "...". There are some discussions of string length and truncation at this PHP strlen manual page.

h.t.h.

FL4PJ4CK’s picture

Hi Jennifer_M,
I think I understood the principle, but I'm not sure where to do this.
I couldn't find the line $title = drupal_get_title(); in the module's folder or somewhere else.
Could you help me with that?

Thank you, Nico

frankcarey’s picture

Title: Shorten or truncate title » Shorten or truncate title - provide theme_breadcrumb override.
Version: 6.x-1.3 » 7.x-1.x-dev
Category: support » feature

I think you are looking for theme_breadcrumb() http://api.drupal.org/api/function/theme_breadcrumb/6

You could add your code to limit the length there.

Considering how generally useful this would be to implement in the module itself, changing to a feature request and updating title. Moving to 7 since i think all feature requests are supposed to go there?

frankcarey’s picture

actually, could probably do this in the module without a theme override, but then it would only effect these breadcrumbs and not all of them, pros and cons to each :)

MGN’s picture

Version: 7.x-1.x-dev » 6.x-2.x-dev

You would override theme_breadcrumb at the theme level. So you would modify your themes phptemplate_breadcrumb (or yourthemename_breadcrumb) function (within template.php in your theme directory). If your theme doesn't have this function, copy the code from theme_breadcrumb and rename it phptemplate_breadcrumb (or yourthemename_breadcrumb) and add it to template.php. Then modify it to check the breadcrumb title length.

This could be done within custom breadcrumbs, but since its easily handled within the theme layer I am not sure its needed. Still, I'll be happy to look at a patch (against 6.x-2.x-dev for now, since it would be for custom breadcrumbs 2) if anyone is interested enough to add this feature.

FL4PJ4CK’s picture

That's awesome, I didn't back on an answer to that topic- thank you!
I'll try this in the near future and report how it works.

FL4PJ4CK’s picture

That's what my function looks like now (if anyone wants to try this, too):

function mw_breadcrumb($breadcrumb) {
  // Determine if we are to display the breadcrumb.
  $show_breadcrumb = theme_get_setting('zen_breadcrumb');
  if ($show_breadcrumb == 'yes' || $show_breadcrumb == 'admin' && arg(0) == 'admin') {

    // Optionally get rid of the homepage link.
    $show_breadcrumb_home = theme_get_setting('zen_breadcrumb_home');
    if (!$show_breadcrumb_home) {
      array_shift($breadcrumb);
    }

    // Return the breadcrumb with separators.
    if (!empty($breadcrumb)) {
      $breadcrumb_separator = theme_get_setting('zen_breadcrumb_separator');
      $trailing_separator = $title = '';
      if (theme_get_setting('zen_breadcrumb_title')) {
        $trailing_separator = $breadcrumb_separator;
        $title = menu_get_active_title();
      }
      elseif (theme_get_setting('zen_breadcrumb_trailing')) {
        $trailing_separator = $breadcrumb_separator;
      }
	  $breadcrump_output = '<div class="breadcrumb">' . implode($breadcrumb_separator, $breadcrumb) . "$trailing_separator$title</div>";
	  if(strlen($breadcrump_output) > 225) {
		$breadcrump_output = substr($breadcrump_output, 0, 225);
		$breadcrump_output = $breadcrump_output. ' [...]';
	  }
      return $breadcrump_output;
    }
  }
  // Otherwise, return an empty string.
  return '';
}

This shortens the breadcrump to a maximum of - in this example - 225 characters and adds [...] at the end of it. That works pretty good for me.

Thanks, MGN!

MGN’s picture

Status: Active » Closed (won't fix)

Glad you found a solution. I am marking this as won't fix since I think you have found the best solution. Custom breadcrumbs provides the data that the theme can use to present the breadcrumb. Modifications like that proposed in this feature request concern the presentation of the data and should really be handled by the theme.

art is code’s picture

Hi there,

Just to write down my solution to this issue : I wanted to limit the number of characters of each breadcrumb (not the overall line of breadcrumbs).
I did this using jquery. I added the following code at the beginning of function mytheme_breadcrumb($breadcrumb) :

drupal_add_js("$(document).ready(function() {
    //In case the overall line of breadcrumbs is bigger than 140 characters - this depends on your needs of course
    if(($(\".breadcrumb\").text().length) > 140) {
      $(\".breadcrumb\").find(\"a\").each(function(i) {
        //Get the number of characters for each breadcrumb
        var breadcrumbLength = $(this).html().length;
        //If the breadcrumb is over 40 characters long
        if (breadcrumbLength > 40) {
          //reduce the breadcrumb to 25 characters and add a trailing "..."          
          $(this).replaceWith($(this).html().substring(0,25)+'...');    
        }
      });
    }  
  });", 'inline', 'header');  

Then I had to add one more condition in order to shorten breadcrumbs only past a certain level (the fifth in my case). So instead of

if (breadcrumbLength > 40) {

I wrote

//i starts at 0
if (i > 3 && breadcrumbLength > 40) {

And that was it !

krem’s picture

Just wanted to share the minimal way to shorten the breadcrumb title of the current page when it gets too long (120 chars in my case)

function mytheme_breadcrumb($breadcrumb){
$title = menu_get_active_title();
if(strlen($title) >= 120){
$title = substr($title,0,120) . ' ...';
}
return '

';
}

Clem

alanpeart’s picture

For anyone looking for a non-javascript way of shortening all individual breadcrumb links (not the entire string) to a given character length, here is how I did it using preg-replace:

	// Truncate breadcrumb links to max 25 characters
	foreach($breadcrumb as &$crumb) {
		$crumb = preg_replace("/>(.*)\<\/a\>/e", "'>'.(strlen('$1') > 25 ? substr('$1', 0, 25).'...' : '$1').'</a>'", $crumb);
	}

Insert this just before the final line in your theme_breadcrumb function in your template.php and you should be good. For anyone who can't read regular expressions easily (me included) what it's doing is looking for the text within the anchor link, and replacing it with a version of itself truncated to 25 characters. If it gets truncated, an ellipsis is added.

If you're adding the current page title to the end of your breadcrumbs, and want to truncate that too, you have to do it separately. It's up to your individual theme where in the function you want to do it but I used the following line to generate my shortened title:

$title = (strlen($title)>25) ? substr($title, 0, 25).'...' : $title;
hanno’s picture

@alanpeart thanks, works perfect.
Based on that here is a solution that trims the breadcrumb respecting word endings. It uses the function truncate_utf8. Last parameter means it will automatically add '...' when needed.

    foreach($breadcrumb as &$crumb) {
$crumb = preg_replace("/>(.*)\<\/a\>/e", "'>'.(truncate_utf8('$1','25',TRUE,TRUE)).'</a>'", $crumb);
}
adshill’s picture

Can anyone help in terms of how I could get this working for Drupal 7? Thanks, Adam.