Be careful with Views Custom Field

Table of Contents

I was tearing my hair out over mysterious bugs in my recent Drupal project. Our complex views pages, which used Views custom field extensively, were constantly breaking. The php field output could break when new CCK field was added to some node type, or some value was selected in views exposed filters.
The code of my phpfields was very simple, something like this:

field_node_status_value > 2) {     echo l("Some action", 'test/url'); } ?>

I've spent several hours debugging the problem. When examining views generated sql queries, I noticed that field alias of 1 specific field can be different under certain conditions. So, instead of $data->fieldnodestatusvalue it could be $data->fielddeliverynodestatus_value. This information helped me to find related issue thread: http://drupal.org/node/701798

It was so surprising to know that you can't rely on $data variable when using php field! Views renames field names all the time (when new field was added to views output, when new field was added to CCK node type, when exposed filter is active, etc, etc), and you simply can't use this raw data! (And that is the intended behavior of Views, merlinofchaos says: http://drupal.org/node/361756 )

I've solved the problem by moving all my PHP code from views custom field to views templates, located in my theme:   {$view->field['field_node_status_value']->field_alias}; if ($status > 2) {     echo l("Some action", 'test/url'); } ?>

I think this problem should be highlighted with yellow background on Views Custom Field module page, but it's not even mentioned there.