Please excuse me if this is a stupid question, but I can't find an answer for it. I want to create missing titles from the first few words from the node body.

So, for instance, if a user enters the following into the body:

American TV viewers have four extra months to get ready for the day their local stations shut down their analog transmitters, thanks to a delay in the digital TV transition approved by Congress Wednesday.

Then the title would read:

American TV viewers have four extra months...

Is there a way to do this with Automatic Nodetitles? Thanks.

Comments

Sorry, I guess this really was a stupid question, since no one answered it. If someone could point out where I might find the answer, I would greatly appreciate it. Thank you very much.

I don't think this is possible since there's no token for body, you could hack the token module to do this, read http://drupal.org/node/181546

However, you can always create a cck text field and you'll have a token for that. You could also do it with php but I wouldn't know how becase the node title would be generating at the same time you're inserting the body into the database, so I wouldn't know how to extract it from the table.

If you find a solution please let me know since I'm also interested. I think I'll go with the cck text field choice.

Thanks. I think the hacking-Token option will work best for my case.

I'd use PHP in the automatic title settings field.

<?php
print node_teaser(str_replace($node->body,'<!--break-->',''), NULL, 32) . '...';
?>

The str_replace can be omitted if you're 100% certain that the teaser break isn't manually added to any of the nodes.

I'm only getting "..." for titles using the code above. Any ideas what I might have wrong? (made sure I had data in the body field and the evaluate PHP option was set in the auto title settings)

I had a brain fart and forgot that str_replace has a wacky argument order:

<?php
print node_teaser(str_replace('<!--break-->', '', $node->body), NULL, 32) . '...';
?>

Actually there is this great little snippet which works brilliantly and may do exactly what you're looking for if I understand correctly.

the way it's written, the snippet will take the first 10 words of the body and make it the title, but you can customize the number of words. check it out and tell us if that's what you want :

http://drupal.org/node/283830

Is the 32 parameter the number of chars?
Could look that up I suppose... or play w/it and see. Lazy. :)

Thanks for link. This does look like what I'm after... though if stormsweeper's code works, it appears to be much simpler. Will do some testing later and post what I find out.

The code in #6 works great and, yes, the '32' is the number of characters. The possible advantage of the solution here http://drupal.org/node/283830 is that it would be using a number of words rather than characters. But I don't need that in my situation.

The function node_teaser() will try to break at a sensible place within the number of characters you specify.

If you have general users creating these nodes, you might want to wrap it in check_plain() to prevent any tomfoolery.

<?php
print check_plain(node_teaser(str_replace('<!--break-->', '', $node->body), NULL, 32)) . '...';
?>

Thanks for the tip. Good idea.

Awesome. Thanks all. Might I also recommend throwing in truncate_utf8().

From the truncate_utf8 Drupal API page...
truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE);

<?php
return check_plain(truncate_utf8(str_replace('<!--break-->', '', $node->body), 80, TRUE, TRUE));
?>

This should work real sexy like.

This works nicely. However, when the title contains a quotation mark, I get "quot". And when it contains an apostrophe, I get #039;. How do I get auto node to translate this into normal punctuation?

Here' s the solution for the quotation problem:

<?php
return htmlspecialchars_decode(truncate_utf8(str_replace('<!--break-->', '', $node->body), 80, TRUE, TRUE), ENT_QUOTES);
?>

I've omitted check_plain() since it doesn't work propably with htmlspecialchars_decode(), don't know why.

#6 works for me, but "..." always show up even the actual number of characters less than the specified character. any idea?

Drupal 7 version of auto node title and token modules allows use of the body token directly.

This is what I use in D7 to generate node title from body:

<?php
if (empty($node->body[$node->language][0]['value'])) $node->body[$node->language][0]['value'] = '';
$out = truncate_utf8(strip_tags($node->body[$node->language][0]['value']), 120, true, true);
return
$out ? $out : 'Node Title';
?>

Version:6.x-1.0» 7.x-1.x-dev

The token doesn't work!