Token for body of node

lordgilman - October 7, 2007 - 20:24
Project:Token
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

Could there be a token implemented that would output the body of the node? I'm looking to use it in the "send to an arbitrary mail address" action when a node is created.

#1

fago - October 12, 2007 - 17:14
Project:Workflow-ng» Token
Version:5.x-1.0» 5.x-1.x-dev

#2

greggles - October 16, 2007 - 01:40
Status:active» postponed

This could be a performance problem with complex nodes. Marking postponed until we figure out token generation performance.

#3

lordgilman - October 18, 2007 - 02:17

Maybe you could use/take a look at the code from the Notify module?
http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/notify/noti...
I think the bit under "Helper function to send the notification email." (search for it) is the code that gets the node's body but I could be wrong.

I was looking into using this node as a solution for what I want Drupal to do with newsletters but token + workflow_ng is a much more elegant way of doing things.

#4

greggles - October 18, 2007 - 10:27

Well, you can make a "node body" token in a module that you use and just make sure that it doesn't get executed all the time by putting it into a specific namespace. Take a look at the way that paypalnode does it: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/paypalnode/...

That only gets executed when token_replace is called with "PayPal Node" as the $type which effectively limits the frequency of that code actually firing to the times it is needed. You can do the same thing to avoid this performance problem.

#5

lordgilman - October 28, 2007 - 00:32

greggles: could this get moved back to the workflow_ng module then? I was sorta asking for a patch for that module anyway.

#6

greggles - October 28, 2007 - 00:59

That's not my module - I can't say. I suggest you submit an issue in its queue.

#7

Amitaibu - November 2, 2007 - 09:00

As a workaround, you can create a field in CCK, called Body and use the token of that field.

#8

fago - November 2, 2007 - 10:59

just for reference, the workflow-ng issue: http://drupal.org/node/188682

#9

archetwist - December 3, 2007 - 16:35

I've "hacked" the token_node.inc file and added the following to the node_token_values function:

<?php
      $values
['teaser']           = check_plain(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->teaser));
     
$values['body']             = check_plain(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->body));
     
$values['body-200']         = substr(check_plain(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->body)),0,200);
     
$values['body-400']         = substr(check_plain(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->body)),0,400);
?>

and, later in the file, under node_token_list:

<?php
    $tokens
['node']['teaser']         = t('Node teaser');
   
$tokens['node']['body']           = t('Node body');
   
$tokens['node']['body-200']       = t('First 200 characters of a body');
   
$tokens['node']['body-400']       = t('First 400 characters of a body');
?>

I am using the teaser and body-400 tokens and do not observe any performance problems. I need these tokens to allow my users to submit the stories they read to digg-like services.

#10

levavie - February 7, 2008 - 13:34

Here is a patch for version 5.x-1.9.5 with archetwist's solution (added body-100 and body-50 tokens).

Amnon
-
Professional: Drupal Israel | Drupal Development & Consulting | Eco-Healing | Effective Hosting Strategies | בניית אתרים
Personal: Hitech Dolphin: Regain Simple Joy :)

AttachmentSize
token.module-token_node.inc_.patch 2.03 KB

#11

tizzo - June 24, 2008 - 16:19
Status:postponed» needs review

I have the same use case described above (workflow_ng send email to users of a certain role) and also need this capability. Is enough known about the performance of token module to reevaluate this?

Are all values populated each time token is invoked? Is what leads to the potential performance problems?

#12

greggles - June 24, 2008 - 16:38
Status:needs review» postponed

Are all values populated each time token is invoked? Is what leads to the potential performance problems?

Yes.

Is enough known about the performance of token module to reevaluate this?

I don't believe so.

#13

grendzy - February 10, 2009 - 22:54

subscribing. I'd like to use notifications module to email $node->body.

Thanks!

#14

tizzo - March 6, 2009 - 17:20

I was looking to do something similar to what grendzy mentioned using workflow_ng to send the body. After looking at the code of token I can see why performance could be a serious issue. That said, the site that I was building has a reasonably small audience and a very small set of people that act as logged in users (most pages are served from the cache) so I decided it wasn't much of a problem.

What I did was take the patch above and drop it into a little body_token module on my site. Token is pluggable so this is easy to add as long as you're aware of the performance risks. I'll attach the D5 version that I made but USE WITH CAUTION!

AttachmentSize
body_token.zip 1.79 KB

#15

opensanta - May 17, 2009 - 08:37
Version:5.x-1.x-dev» 6.x-1.x-dev
Status:postponed» needs work

@tizzo: Can you please post this as a patch, possibly for D6?

#16

opensanta - May 17, 2009 - 21:07
Status:needs work» postponed

nevermind #15.
#7 is the best short-term solution, and as greggles noted in #2, this would require other performance related improvements to token.

#17

jvieille - August 17, 2009 - 06:47

We really need to be able to post Nodes body.
I don't really catch the performance issue, a warning in the documentation would address this concern.

I changed my Token module in D6 as in #7, this is what is needed (just be aware of the plain text transformation effect)

A CCK field is not a solution as it would mean to modify existing node types and messing things between past published nodes and new ones.

This is a old issue...

#18

jvieille - August 18, 2009 - 16:52

Some improvement needed for the patch:

If the body contents does not render non-alphanumeric characters.

For example, the following text
"Présentation - Ergonomie"

is received like this:
"<h1>Pr&eacute;sentation - Ergonomie</"

I replaced
substr(check_plain(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->body)),0,50);
by
substr(preg_replace(array('/\<\/p\>/','/\n/'),' ',$node->body),0,50);

but it does not help much:

"

Présentation - Ergonomie

Pr&e"

#19

marcushenningsen - August 19, 2009 - 07:56

I'm using the workaround in #9 and it's working very well. Is there any reason this shouldn't be implemented, at least the shortened versions, or might they also generate perfomance issues?

#20

greggles - August 21, 2009 - 00:16
Status:postponed» needs work

The reason we aren't doing this is performance.

I don't really catch the performance issue, a warning in the documentation would address this concern.

A warning in the documentation that says "this module, the 3rd most popular one in Drupal, will slow down your site unnecessarily." That just isn't acceptable.

Now, I'm re-opening this because someone could provide a patch for the tokenSTARTERKIT. I'd commit that.

#21

jvieille - August 22, 2009 - 05:42

Is performance affected even if the new body tokens are not used?
For example, the "raw user input tokens" have a warning in the code itself, so the user knows when selecting them about possible drawbacks

#22

grendzy - August 22, 2009 - 15:50

Yes, it seems hook_token_values() computes values for all possible tokens, without regard to whether they are actually used or not. I don't think that can be changed without changing the API.

#23

grendzy - August 24, 2009 - 05:33

Here is something interesting:

adding

<?php
$values
['content'] = node_view($node, FALSE, TRUE);
?>
to
<?php
node_token_values
()
?>
actually made it faster by a tiny, but seemingly repeatable amount.

As a baseline, 10,000 calls to token replace took 7.098 seconds. After adding the above token, it took 6.976 seconds.

This is with a core page node. I haven't benchmarked it with CCK fields yet, but perhaps the performance worries around this issue are overstated.

 
 

Drupal is a registered trademark of Dries Buytaert.