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.


kirkcaraway’s picture

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.

lelizondo’s picture

I don't think this is possible since there's no token for body, you could hack the token module to do this, read

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.

kirkcaraway’s picture

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

stormsweeper’s picture

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

print node_teaser(str_replace($node->body,'',''), 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.

aharown07’s picture

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)

stormsweeper’s picture

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

print node_teaser(str_replace('', '', $node->body), NULL, 32) . '...';
poloparis’s picture

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 :

aharown07’s picture

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

aharown07’s picture

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.

aharown07’s picture

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

stormsweeper’s picture

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.

print check_plain(node_teaser(str_replace('', '', $node->body), NULL, 32)) . '...';
aharown07’s picture

Thanks for the tip. Good idea.

doublejosh’s picture

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);

 return check_plain(truncate_utf8(str_replace('', '', $node->body), 80, TRUE, TRUE));

This should work real sexy like.

abaltaz’s picture

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?

Bachir’s picture

Here' s the solution for the quotation problem:

return htmlspecialchars_decode(truncate_utf8(str_replace('', '', $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.

Guo’s picture

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

mlncn’s picture

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

unifiedac’s picture

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

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';
Jumoke’s picture

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

The token doesn't work!

olisb’s picture

Issue summary: View changes

The token [node:body] does not work, but @unifiedac's code from #18 does, and you can use it to pull any other field your user might be creating by changing 'body' to 'my_field_name'