Creating title from node body text

kirkcaraway - February 5, 2009 - 15:15
Project:Automatic Nodetitles
Version:6.x-1.0
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

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.

#1

kirkcaraway - February 21, 2009 - 17:57

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.

#2

lelizondob - March 22, 2009 - 13:11

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.

#3

kirkcaraway - March 23, 2009 - 15:17

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

#4

stormsweeper - March 24, 2009 - 02:51

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.

#5

aharown07 - March 24, 2009 - 15:08

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)

#6

stormsweeper - March 25, 2009 - 20:36

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) . '...';
?>

#7

poloparis - March 25, 2009 - 22:29

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

#8

aharown07 - March 26, 2009 - 04:11

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

#9

aharown07 - March 26, 2009 - 04:14

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.

#10

aharown07 - March 27, 2009 - 21:58

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.

#11

stormsweeper - April 6, 2009 - 17:38

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)) . '...';
?>

#12

aharown07 - April 6, 2009 - 21:20

Thanks for the tip. Good idea.

#13

doublejosh - April 28, 2009 - 19:36

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.

#14

abaltaz - September 6, 2009 - 00:33

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?

 
 

Drupal is a registered trademark of Dries Buytaert.