I have a list of nodes that I show in a block and I'm trying to trim the title because sometimes it overflows a line and goes to two lines and this messes up the block formatting, so I need to limit the title that is displayed to 25 characters or less. Anyway, I couldn't figure out any way to do this so I'm trying to use MySQL functions to do this. The MySQL query works when I run it, but when I put it in my page I'm getting zero output and no errors printing. Can anybody spot something wrong here (or come up with an easy way to do this)? To summarize, I'm trying to print the title if it's less than 25 characters and if not, I find the last space, get a substring up to that (so I don't split words) and than concatonate a '...' on it to indicate it's been truncated. I then try to load that manually via db_fetch_object rather than using node_load.
Here is my original page that works:
<?php
/**
* This php snippet displays content of a specified type, with teasers,
* from certain taxonomy term.
* Sorted by date of creation, most recent first.
* Works with nodes of the flexinode type too.
* To change the length of the list, change $listlength.
* To change the taxomony term, change $taxo_id.
* To change the type of content listed, change the $content_type.
* Tested with Drupal 4.6.3
*/
$listlength="5";
$taxo_id = "1,2,3,4";
$content_type = 'story';
$result1 = pager_query(db_rewrite_sql("SELECT DISTINCT n.nid, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid IN ($taxo_id) AND n.status = 1 ORDER BY n.created DESC"), $listlength);
while ($node = db_fetch_object($result1)) {
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
}
print $output;
?>
Here is the page where I tried to trim the titles with raw sql that does not work:
<?php
/**
* This php snippet displays content of a specified type, with teasers,
* from certain taxonomy term.
* Sorted by date of creation, most recent first.
* Works with nodes of the flexinode type too.
* To change the length of the list, change $listlength.
* To change the taxomony term, change $taxo_id.
* To change the type of content listed, change the $content_type.
* Tested with Drupal 4.6.3
*/
$listlength="5";
$taxo_id = "1,2,3,4";
$content_type = 'story';
$result1 = pager_query(db_rewrite_sql("SELECT DISTINCT n.nid, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid IN ($taxo_id) AND n.status = 1 ORDER BY n.created DESC"), $listlength);
while ($node = db_fetch_object($result1)) {
$output .= node_view(db_fetch_object(db_query('SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.sticky, r.timestamp AS revision_timestamp,
CASE
WHEN CHAR_LENGTH(
r.title
) <25
THEN r.title
ELSE CONCAT( SUBSTRING( r.title, 1, ( 25 - LOCATE( ' ', REVERSE( SUBSTRING( r.title, 1, 25 ) ) ) ) ) , '...' )
END title,
r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE nid=' . $node->nid)), 1);
}
print $output;
?>
Comments
Try this
Search for drupalicious to find the node that holds the snippet.
Thanks for that snippet.
Thanks for that snippet. How do I integrate that with node view? Do I need to create my own node view function that will trim? If I do and place it in a template file can I still call it from my php snippet to create my block?
Just in case anybody else
Just in case anybody else has this same problem, I simply created a custom function in a php file called "trimmed_node_load" and copied out the standard node_load code and added my sql changes. Not ideal, but it got me what I wanted. If anybody wants a copy of the file, feel free to send me an email.