Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
kirkcaraway CreditAttribution: kirkcaraway commentedSorry, 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.
Comment #2
lelizondo CreditAttribution: lelizondo commentedI 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.
Comment #3
kirkcaraway CreditAttribution: kirkcaraway commentedThanks. I think the hacking-Token option will work best for my case.
Comment #4
stormsweeper CreditAttribution: stormsweeper commentedI'd use PHP in the automatic title settings field.
The str_replace can be omitted if you're 100% certain that the teaser break isn't manually added to any of the nodes.
Comment #5
aharown07 CreditAttribution: aharown07 commentedI'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)
Comment #6
stormsweeper CreditAttribution: stormsweeper commentedI had a brain fart and forgot that str_replace has a wacky argument order:
Comment #7
poloparis CreditAttribution: poloparis commentedActually 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
Comment #8
aharown07 CreditAttribution: aharown07 commentedIs the 32 parameter the number of chars?
Could look that up I suppose... or play w/it and see. Lazy. :)
Comment #9
aharown07 CreditAttribution: aharown07 commentedThanks 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.
Comment #10
aharown07 CreditAttribution: aharown07 commentedThe 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.
Comment #11
stormsweeper CreditAttribution: stormsweeper commentedThe 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.
Comment #12
aharown07 CreditAttribution: aharown07 commentedThanks for the tip. Good idea.
Comment #13
doublejosh CreditAttribution: doublejosh commentedAwesome. 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);
This should work real sexy like.
Comment #14
abaltaz CreditAttribution: abaltaz commentedThis 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?
Comment #15
Bachir CreditAttribution: Bachir commentedHere' s the solution for the quotation problem:
I've omitted check_plain() since it doesn't work propably with htmlspecialchars_decode(), don't know why.
Comment #16
Guo CreditAttribution: Guo commented#6 works for me, but "..." always show up even the actual number of characters less than the specified character. any idea?
Comment #17
mlncn CreditAttribution: mlncn commentedDrupal 7 version of auto node title and token modules allows use of the body token directly.
Comment #18
unifiedac CreditAttribution: unifiedac commentedThis is what I use in D7 to generate node title from body:
Comment #19
Jumoke CreditAttribution: Jumoke commentedThe token doesn't work!
Comment #20
olisb CreditAttribution: olisb commentedThe 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'
Comment #21
anmolgoyal74 CreditAttribution: anmolgoyal74 at OpenSense Labs for DrupalFit commentedcomment #18 works fine.
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';
you can replace 'Node Title' with any other default value in case body case is empty.
Comment #22
Utkarsh_Mishra CreditAttribution: Utkarsh_Mishra at OpenSense Labs for DrupalFit commentedComment #23
gaurav.kapoor CreditAttribution: gaurav.kapoor commented