Archive for the ‘Drupal’ Category

Limiting Node Title Lengths In Drupal

Friday, December 12th, 2008

Im just wrapping up development on WinForAYear.com and because of how I’ve formatted the nodes, I needed a way to shorten the titles if they exceeded a set length.  This feature would come in handy in many situations, like presenting a table of stats where the title could easily overlap into another column.

Now you could easily edit the title css to include a set width with “overflow:hidden” hiding the excess, but then you would be left with abruptly ending titles with no indication that there is more.  However, if there were “…” attatched to the end of the shortened titles, things would be more clear.

The Solution
Open up your node.tpl.php file in your drupal theme.  Look for this line:

<?php print $title; ?>

and replace it with:

<?php
// if title is longer than 28 characters, cut off the rest and add “…”
if(strlen($title) > 28){ print substr($title, 0, 28).”…”;}
else { print $title; }
?>

The Breakdown

if(strlen($title) > 28)

If the title is greater than 28 characters (including spaces).

{print substr($title, 0, 28).”…”;}

Display the first 28 characters of the node title and attatch “…” to the end.

else { print $title; }

Now we don’t want “…” being displayed if the title isn’t too long, so here we just display the title unchanged.

New Regions Not Loading in Drupal 6

Tuesday, October 28th, 2008

Creating new regions in a Drupal theme that you can publish blocks too, is extremely easy.

edit your “themename.info” file:

regions[name_of_new_region] = name of new region

then output the region in your “page.tpl.php” file:

<?php if ($name_of_new_region) print $name_of_new_region; ?>

The problem  you may run into (and the point of this post), is that during development of your theme, the new regions may not show up in your Drupal blocks page.  What you will need to do, is head over to your “build/themes” page and set another theme as your default temporarily.  Once another theme is saved as the default, reselect your theme and set it as the default once again.  This will load your new regions, and you should now be able to publish blocks to them.

I didn’t see mention of this problem on other sites, so I thought I would throw it out there in case anyone else had the same frustration!