I want to limit the number of letters displayed in certain blocks that I have created. The titles on some stories are way too long creating a ridiculous looking table.

Thanks

Comments

scoutbaker’s picture

Duplicate of http://drupal.org/node/359543. Please don't post the same question multiple times.

---
"Nice to meet you Rose...run for your life." - The Doctor
My first public Drupal site - EyeOnThe503

sammyman’s picture

Thanks, I obviously posted the other post in the wrong forum.

hectorplus’s picture

Developing a module that would check for doops, would be nice, i am fairly new to drupal, but willing to put some money.

HectorPlus.com

About me - Beyond me.

dnewkerk’s picture

To limit at the time of entry, check http://drupal.org/project/maxlength (the 6.x dev version works fine)

To "trim" the title no matter what, you need a little PHP in your template.

Here's an example:

<?php
// Limit $title characters, shorten and include ... when necessary
  $limit = 22;
   if (strlen($title) > $limit)
      $title= substr($title, 0, strrpos(substr($title, 0, $limit), ' ')) . '...';
?>

<p><?php print $title; ?></a></p>

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

sammyman’s picture

I think I will need to implement the php in the template. But the question is... which template? If I want to edit a block for instance?

dnewkerk’s picture

What is generating the block? Is it a block that came with Drupal core? Did you make it with Views? Did the block come from another contributed module?
If it's a View block then you can use Views template files (check the Theme section of Views).

Other blocks, here's how you make a template for a specific block:
http://drupal.org/node/190815#template-suggestions

One simple example is enabling the Recent Comments block. Copy and paste the block.tpl.php file in your theme (or from any theme if yours doesn't have one) and name it block-comment.tpl.php. Type a bit of text in it, save the file and reload the page. You should see your text, but "only" in the Recent Comments block. It's possible you may have to clear the theme registry to see the new template... if you don't see anything, go to Site configuration > Performance and clear the cache.

Also be sure to try out the Devel module's "Theme Developer" functionality, which lets you click on things on the page and tells you about how to theme them.

If you put this bit of code temporarily into your block template, it will also spit out a lot of details about the underlying guts of the block:

<pre>
  <?php print_r($block); ?>
</pre>

You can use the same bit of code in other templates to learn about what's going on behind the scenes, plugging in whatever variable is appropriate for that template (e.g. on node templates you could use $node).

All that said, I'm not certain how to make use of that PHP code I gave you in the context of a pre-generated block (if that's what you're trying to do), since I don't see the node titles being revealed in the [content] part (I found this post that indicates that blocks at that stage are just receiving already-rendered HTML that you can't change any more at that point). I'm not too sure on this area, because I only use custom blocks I make in Views. If your block is from Views though, should be no problem as Views gives you many template options ranging from the whole View down to one for every separate field.

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

sammyman’s picture

Your post was really informative. I have no problem using views to output my specific files. When I go to the theme section of the views page, I find in one example:

Style output: views-view-table.tpl.php, views-view-table--my-top-rated.tpl.php, views-view-table--default.tpl.php, views-view-table--my-top-rated--default.tpl.php

I know where views-view-table.tpl.php is in the module/views/theme/ folder, but where are the rest of these custom tpl.php files? And how can I let Views know I want to use a custom one, rather than the default first bolded file like views-view-table.php? I can't seem to find anything in the views > theme information section that allows me to specify the custom file.

Thanks!

dnewkerk’s picture

What you do is make a new blank file in your theme's directory, and then name it by the name you want to use from those suggested by Views... the more specific name you choose, the more specific of the View or "part of a View" it will affect. Once a file exist in your theme's directory by one of these names, you click the "Rescan Template Files" button and Views will see it. Copy and paste the content of the template (from the link, e.g. Style output). You can then modify that template as much as you like. You can also use more than one template (e.g. one for more overall theming of the view, and another to customize one particular field).

Don't modify the copies of the templates in the Views modules directory though... those are the "defaults" which you make your own templates in your own theme's folder to override.

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides

sammyman’s picture

Got it to work! You are the man! You can't believe how excited I am. Now it is off to tweaking the code line by line. Sweet!

Edit: I tried this code to limit the content to only 5 characters but it didn't work :(

// Limit $title characters, shorten and include ... when necessary
  $limit = 5;
   if (strlen($content) > $limit)
      $contect= substr($content, 0, strrpos(substr($content, 0, $limit), ' ')) . '...';

print $content;

And this:

    // Limit $summary to how many characters?
   $limit = 5;
   if (strlen($content) > $limit)
      $content = substr($content, 0, strrpos(substr($content, 0, $limit), ' ')) . '...';
print $content; 
dnewkerk’s picture

$content is not likely a field you can do that on... e.g. if it's $content in a node, then $content is the "combination" output of all the other fields. You can only use that PHP code on a variable that's outputting a final chunk of text (versus a variable that's printing out other variables). To get inside of $content (at least in the context of nodes), check this lesson I wrote: http://www.davidnewkerk.com/book/30

-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides