I will like to limit long node titles to first 'n' characters in a side block ( which is narrow ) for example in 'recent blog posts' block

else if ($op == 'view') {
    if (user_access('access content')) {
      $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, 5);
      if (db_num_rows($result)) {
        $block['content'] = node_title_list($result);
   $block['content'] .= '<div class="more-link">'. l(t('more'), 'blog', array('title' => t('Read the latest blog entries.'))) .'</div>'; 
        $block['subject'] = t('Recent blog posts');
        return $block;

For example, in the above how do I manipulate node_title_list with something like substr() to truncate long or very long titles?

Regards

Comments

dnewkerk’s picture

Here's a snippet of code I used once in a Views template, to shorten a node title... maybe it will help:

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


<div class="photo-title">
  <?php print l($title, "node/$node->nid", $attributes, null, null, false, true); ?>
</div>

-- David
absolutecross.com
[new guide/lesson in progress: Creating a CCK and Views powered Drupal site - feedback welcome]

minesota’s picture

Hello, thanks a lot.
Any little help on how I can merge this into node_title_list ?

vijaythummar’s picture

If you want to change in directly node.module then there is

function node_title_list($result, $title = NULL) {
  $items = array();
  $num_rows = FALSE;
  while ($node = db_fetch_object($result)) {
    $items[] = l($node->title, 'node/'. $node->nid, !empty($node->comment_count) ? array('attributes' => array('title' => format_plural($node->comment_count, '1 comment', '@count comments'))) : array());
    $num_rows = TRUE;
  }

  return $num_rows ? theme('node_list', $items, $title) : FALSE;
}

where you can change the length of node title like

function node_title_list($result, $title = NULL) {
  $items = array();
  $num_rows = FALSE;
  while ($node = db_fetch_object($result)) {
    $items[] = l(substr($node->title, 0, 50), 'node/'. $node->nid, !empty($node->comment_count) ? array('attributes' => array('title' => format_plural($node->comment_count, '1 comment', '@count comments'))) : array());
    $num_rows = TRUE;
  }

  return $num_rows ? theme('node_list', $items, $title) : FALSE;
}

But better way is make your own block or you can also chage the theme function of theme_item_list .

Regards,
Vijay

Thanks,
Vijay Thummar

minesota’s picture

Hello and regards

Thanks a LOT. That is a very useful piece of code.

[ There is one problem I am facing, with titles in unicode characters substr($node->title, 0, 50 is making certain titles just blank. Yet to figure out why this is happening. For example, with substr($node->title, 0, 40 the truncated title is there, with substr($node->title, 0, 60 the title just vanishes!

Edited : This problem is solved by using drupal_substr instead of substr. I guess truncate_utf8 can be used also, but no idea how that can be fitted in the above code]

theorichel’s picture

where I should paste this piece of code please to alter my Recent Comments block (Views)?

Many thanks

Theo Richel

yuriy.babenko’s picture

In your theme's template.php, add:

function _phptemplate_variables($hook, $vars = array())
{ 
	switch($hook)
	{
		case 'node':
		
			if($vars['node']->region == 'right') //replace 'right' with the name of your region
			{
				//replace '100' with the max number of characters you want to display
				$vars['node']->title = _cut_string($vars['node']->title, 100); 
			}
		
			break;		
	}
}

function _cut_string($string, $length = 160)
{
	if(strlen($string) > $length)
	{
		$string = substr($string, 0, $length);
		$pos = strrpos($string, " ");
		
		if($pos === false)
			return $string;
		
		$string = substr($string, 0, $pos);
		$string .= "...";
	}
			
	return $string;
}

This should do the trick, and without any hacking of the Drupal core. (Code not tested.)

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

hansrossel’s picture