I can insert the following function in a block directly, using the PHP filter, but if inserted in Views Custom Field displayed in a block it bombs with a parse error:

<?php
function writeName()
{
echo "Druplicate";
}

echo "My name is ";
writeName();
?>

Parse error: syntax error, unexpected T_ECHO, expecting '{' in /usr/local/www/apache22/data/includes/common.inc(1695) : eval()'d code on line 4

I've used VCF for other PHP before with no problem, but anytime I try to define a function it fails.

Comments

eL’s picture

May be, I have same problem. But if I declare a function in PHP Custom Field, my server crashed with error 500.

eL’s picture

Title: Defining PHP functions bombs with parse error. » Defining PHP functions inside Views Custom Field PHP cause error
Version: 6.x-1.0 » 6.x-1.x-dev

in DEV same problem

eL’s picture

It looks like minor problem, because we can define our function in own modul and use it in custom field. BUT, if we need values from concrete fields from our view, we are screwed. And this is very usefull ie. for Popup module...

casey’s picture

Category: bug » support

The code is run for every row; your function is redefined for every row. If you want functions in your code you have to ensure they don't get redefined. You could use function_exists or the $static variable to do so.

eL’s picture

casey - it was a good hint!

Anyway, we can declare function in own helper module and pass variables through function arguments.

tekken’s picture

thanks casey.