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
Comment #1
Jennifer_M commentedFWIW (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.
Comment #2
FL4PJ4CK commentedHi 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
Comment #3
frankcarey commentedI 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?
Comment #4
frankcarey commentedactually, 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 :)
Comment #5
MGN commentedYou 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.
Comment #6
FL4PJ4CK commentedThat'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.
Comment #7
FL4PJ4CK commentedThat's what my function looks like now (if anyone wants to try this, too):
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!
Comment #8
MGN commentedGlad 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.
Comment #9
art is code commentedHi 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) :
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
I wrote
And that was it !
Comment #10
krem commentedJust 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
Comment #11
alanpeart commentedFor 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:
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:
Comment #12
hanno commented@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.
Comment #13
adshill commentedCan anyone help in terms of how I could get this working for Drupal 7? Thanks, Adam.