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.
An odd bug. If you use functions within PHP code on a content type created with the content construction kit the page itself will fail and all processing seems to stop and only the output of the page is output.
Try it:
create a new form of content with a title and body
Use the content to create a simple page with PHP code in it (don't forget the input format).
add a simple function ex:
function hello-world() {
return "hello";
}
Comment | File | Size | Author |
---|---|---|---|
#6 | drupal_eval.png | 46.18 KB | JohnAlbin |
Comments
Comment #1
jonathan_hunt CreditAttribution: jonathan_hunt commentedUsing // $Id: content.module,v 1.56.2.5 2006/08/04 13:47:50 JonBob Exp $
I just tested this by adding the hello_world function to a CCK content type but I get a fatal error:
Fatal error: Cannot redeclare hello_world() (previously declared in /home/jonathan/public_html/drupal47/includes/common.inc(1150) : eval()'d code:4) in /home/jonathan/public_html/drupal47/includes/common.inc(1150) : eval()'d code on line 6
Comment #2
jonathan_hunt CreditAttribution: jonathan_hunt commented@jredding: Note that '-' is not a valid character for PHP function names (refer http://nz.php.net/manual/en/language.functions.php ). But I still get a fatal error when the function is named correctly... (see previous comment).
Comment #3
killes@www.drop.org CreditAttribution: killes@www.drop.org commentedcan confirm this. Probably the code gets evaluated twice.
Comment #4
jredding CreditAttribution: jredding commentedSorry about my sloppy example I was just typing up something quick. Like you said even if named correctly the bug is still present.
Any ideas? When I get some spare time I'll try to track this down..
Any suggestions on where to look?
Comment #5
jredding CreditAttribution: jredding commentedwhoops, didn't mean to change the title.. my fault.
Comment #6
JohnAlbinThis may be a core bug. I don't have CCK installed, but when I enable nodewords and put a function in a "page" content, I get the same error as you are describing.
I don't know what API is causing drupal_eval() to being called twice, but here's screenshots of the PHP stack during the two calls. Hopefully it will help someone.
Comment #7
RayZ CreditAttribution: RayZ commentedChanging title back.
Comment #8
nevets CreditAttribution: nevets commentedThis is likely do to the fact that the node gets processed twice, once for the teaser and once for the body. I get around it by setting the teaser to a text message.
Comment #9
KarenS CreditAttribution: KarenS commentedI ran into this and reported it somewhere else, but I've lost track of where. It's because each field is evaluated at least twice, once by the field module and once by the content module. This is because the content module has to have a crack at the field info, too, so I don't think the fact that the field is massaged twice is a bug exactly. And it's possible that it gets massaged more than twice (the widget may have a shot at it too, maybe others).
My immediate workaround was to wrap any functions with if (!function_exists('myfunction'). The only other way to fix it is somehow to figure out exactly which pass through the field should trigger the eval, instead of evaling on every pass, but I'm not sure which would be the right time to do that. It might be easier just to check for function_exists.
Comment #10
joeguru CreditAttribution: joeguru commentedThis isn't just happening with the CCK, and it's taking recently-upgraded sites pretty much down if they embed php code. I'm following up on the teaser re-eval theory.
Comment #11
dopry CreditAttribution: dopry commentedI'm setting this to won't fix.... You have to be careful when declaring functions in nodes... I would even go so far as to say you should never do that... You should also be careful that you paste bug free code into nodes.
Comment #12
dman CreditAttribution: dman commentedthis is not a bug, just a result of the PHP language scoping.
PHP would always do that if you included the same library more than once.
It's been a side-effect of all versions of Drupal that allow multiple evaluations of embedded code that are odd enough to try and declare functions inside an eval() context.
try not to do that, or if you must, protect yourself with
But really, seriously, don't try making code that way.