Problem with leeching to CCK node type
hbfkf - May 1, 2007 - 11:02
| Project: | Leech |
| Version: | 5.x-1.x-dev |
| Component: | node_template |
| Category: | bug report |
| Priority: | normal |
| Assigned: | alex_b |
| Status: | needs review |
Description
I have a CCK node type with a teaser CCK text field and want the leeched title to go into both the node's title and this CCK field (i.e., I set the teaser to the title).
To accomplish this, I tried to enter some PHP code into the 'Saving PHP code' field of my template. However, already when I enter
<?php
error_log($node->body, 0);
?>nothing gets output!?
Taking a look at node_template's code, I see in its function node_template_save() that the '$node' argument is NOT set to the function's incoming '$node' argument but to the template node:
// HERE, $node->body is what I'd expect...
if (isset($node->node_template) && $node->node_template->php_save) {
// Run php code for template. Make node "local" for that code.
// BUT IN THE EVAL, $node->body IS DIFFERENT.
drupal_eval('<?php global $node_templates; $node = &$node_templates['.$node\
->node_template->nid.'];?>'.$node->node_template->php_save);
}Why? I don't get the logic here, sorry. Could anybody enlighten me, please?

#1
Does anybody have an idea how to tackle this?
I've checked again the README of Leech, where it says that one can use
<?php$node->field_body[0]['value'] .= $node->body;
?>
But in my case $node->body is empty.
I appreciate any help!
Kaspar
#2
Hi,
I'd like to second.
I have the same problem. I'm using Drupal v5.1 and Leech v5.x-1.8 .
In an essence - I have an RSS Source which fires up an templates of a custom node type (with CCK). So, I need to map the leeched fields of information to my custom CCK node type. I followed the instruction on the README as well as those shown on the screencast (http://youtube.com/watch?v=nRmd6whQFlY). Nada - nothing gets into place and actually, as far as I can see, I don't have the relevant $node object information in scope when my code is running.
I've debugged this a bit:
1. Drupal watchdog - I've put the following code inside my template's "Saving PHP code" section:
<?php$all_vars = get_defined_vars();
$out1 = var_export($all_vars, TRUE);
$out2 = var_export($node, TRUE);
watchdog("leech ALL VARS", $out1);
watchdog("leech NODE", $out2);
?>
Running the RSS leeching proved that neither "all variables" or the $node itself contained the meaningful fetched data for a specific RSS item.
2. I've checked this with a debugger (Zend Studio). The finding are as follows:
_leech_news_save_items()(leech.module, line 2347) callsnode_template_save()(node_template.module).==> This function, in line 89 checks if the template has "php_save" property, meaning a code to run when saving the node. Cool. Now, up to this line, including, the $node object contains all the needed info for this node, including the current leeched item (body and title properties are fed alright, etc). Now, in line 91 there's the drupal_eval() call which supposed to run this code, with some extra code that's supposed to bring into scope the $node variable, so we could "play" with it according to our needs. That is not working properly. While running our code, as seen in (1) above, we do not have the needed information in our disposal.
I think this is a bug, and therefore I'm changing this issue's category.
I suspect this issue is strongly related to this: http://drupal.org/node/145816 (although the latter is for the v4.7 module)
Thanks for the assistance!
Boaz.
#3
Hi,
Well, after further investigation I learned that there are some issues with drupal_eval(), though I'm not sure they're related. It seems that drupal_eval() is hiding any variables of the program from the code its "eval'ing", on purpose. Some findings:
Issues:
http://drupal.org/node/153667
http://drupal.org/node/119075
API documentation:
http://api.drupal.org/api/function/drupal_eval/5
The solution I've devised is to change the drupal_eval() to eval(). It might spell security issues but I'm not sure, in this case as it requires crafting special, somewhat complicated RSS feeds on the RSS sources, and only on these sources.
I'm attaching a patch of the "fix". I'd love feedback... :-)
#4
Thanks, boazry for the patch - will check it out ASAP.
#5
I just looked at the code:
* Your suggestion seems generally fine. I am not entirely sure about the security ramifications.
* Did you try wether this would work?
<?php$GLOBALS['node_template_current'] = &$node; // declare it global this way, otherwise ("global $var; $var =&$node") it doesn't work!
drupal_eval('<?php global $node_template_current; $node = $GLOBALS['node_template_current] ;
?>
?>
* There is also a similar call for ->php_load, we should do the same there, shouldn't we?
* Can you roll the patch against the latest CVS version with cvs diff -u ?
Thanks again for your input. Very much appreciated.
#6
Alex,
Thanks for the quick reply. Here goes:
* well, the code you're supplied is garbaged a bit but it worked! What I implemented, finally, was:
drupal_eval('<?php global $node_template_current;' . '$node = $GLOBALS[' . '\'node_template_current\'];' . (string)$node->node_template->php_save);which is a bit different written, but follows exactly your direction. Probably my lack of experience in PHP lead me to forget the option to put the $node in $GLOBALS... :-| . BTW, the line of code needed to be split into so many parts since otherwise PHP would complain about syntax issues.* About the "load" operation - yes, definitely we should the same thingy there. Otherwise the $node is simply not visible in the scope of the "external" code being run.
* Attached the updated patch, against the latest version from CVS (July31). I know that Drupal site recommends running diff with "-up" flags but as your request - did it with "-u" only.
Thanks again.
#7
One important addition: the code in the "save code" section of the template node should NOT include php tags. Including them aborts the code as "eval()" is getting from Drupal a string like "<?php ... <?php..." -> two opening tags, and aborts its execution. This applies, of course, only after applying the last patch I've attached (which is the fix you suggested).
The fix is therefore only in the documentation of the module. I would have also updated more explicit instructions in the form in which the code is being fed (the "save code ").
#8
Just tested. Looks good on PHP4 over here.
I also applied the same adjustment to node_template_load() - seems to work fine.
I did no applied testing (with a real application of node_template) because I do not use this in any project. boazry, before we can commit this patch, could you test it on your system? Do you happen to have a PHP 5 environment in place? I would love to see a test on PHP 5, too.
Alex
#9
Hi Alex,
All my correspondence above is for php 5 . Sorry if it wasn't clear before. The patch name includes '4' that stands for 'for'... :-) (so the patch name would be descriptive - "node template patch for CCK").
So, as far as I'm concerned, both my initial report on this issue as well as the fix-patch were tested on PHP 5 (v5.2.3, to be exact).
Boaz.
#10
I attached a wrong patch in my mail before. Here it is now: this one implements node load and node save template and adds some better help text. Could you test it on your PHP5 system?
I tested the patch on PHP4.4.2 and it looks fine. I just sent a mail to marcin konicki who is also using this feature (I think) to take a look at it.
#11
Dude, that's pretty embarrassing - takes me 3 times to upload the patch. My FTP client is abandoning me.
#12
boazry, did you have any time to test the patch in #11?
#13
Hi,
Sorry - It had to wait till (my) Friday.
Patch in comment #11 is fine (on PHP v5.2.3). Actually, its the same as what's already running on my machine. Important: I haven't tested the "load code" section. I guess it can be safely passed but had to mention that. Other than that - clear for takeoff.
Nice work,
Boaz.
#14
Thanks, boaz_r I committed to 5.x branch: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/leech/node_...
#15
#16
[code]
$GLOBALS['node_template_current'] = &$node;
drupal_eval('<?php global $node_template_current;' . '$node = $GLOBALS[' . '\'node_template_current\'];' . (string)$node->node_template->php_save);
[/code]
Works for me on PHP5
but to have it work on PHP4 I had to put in an extra & for the node reference:
[code]
$GLOBALS['node_template_current'] = &$node;
drupal_eval('<?php global $node_template_current;' . '$node = &$GLOBALS[' . '\'node_template_current\'];' . (string)$node->node_template->php_save);
[/code]
#17
Sorry if this is a bit off-topic, but are you sure that this issue is related to CCK?
I installed leech in a vanilla 5.3 install and cannot make template save/load php code work on simple node types at all, even after applying the patch above.
Just for testing, I did the following in the load PHP:
$node->title .= ' changed in load';And in the save area:
$node->title .= ' changed in save';I confirm that $node is getting the right content (I tested it using the var_export()/watchdog() technique)), but nevertheless the changes made in $node go nowhere.
Isn't that the way it was supposed to work?