Here's a strange error: adding a CCK field changes existing fields name. I had a custom PHP field calculating things just fine, using data from CCK fields. It used a field which was refered to by the following code:

$number_6th_edition = (int) $data -> node_data_field_number_of_problems_field_number_of_problems_value;

However, after added a new CCK text with the CCK field name field_subtitle, and, based on what I learned using
print_r($data);
the above code needed to change to:

$number_6th_edition = (int) $data -> node_data_field_subtitle_field_number_of_problems_value;

There are no typos above, and everything works fine, but this certainly is not intended behaviour and thought it might be worth reporting.

Summary: adding CCK fields can break existing PHP custom fields be renaming those preexisting fields in unpredictable ways.

Comments

restyler’s picture

Component: Code » Documentation
Priority: Normal » Major

I've spent a lot of hours today struggling with this misterious issue.

And here is the explanation:
that is the intended behavior of views, it renames field aliases in $data field as it wants.
In my case, alias was changed when I've changed my exposed filter value.
Related issue: http://drupal.org/node/361756

So, Views custom field module description (and README) needs to to say in huge letters that $data array in phpfield is NOT the thing you can rely on.

restyler’s picture

Here is the thread - http://drupal.org/node/701798

casey’s picture

In Drupal 7 version, http://drupal.org/project/views_php I use the same variable names as tokens and included an fieldset on the configuration form containing (clickable) available variables.

akobashikawa’s picture

A way to prevent this is changing:

$data->some_field_alias

for this:

$data->{$this->view->field['field_id']->field_alias}

You can find field_ids using mouse over field name in Fields region of Views UI.

A real example:

$cliente_nid = $data->{$this->view->field['field_client_nid']->field_alias};
$pieza = $data->{$this->view->field['field_job_pieza_value']->field_alias};
$descuento = $data->{$this->view->field['field_job_descuento_value']->field_alias};
$factor = $data->{$this->view->field['field_job_factor_value']->field_alias};
$cantidad = $data->{$this->view->field['field_job_cantidad_value']->field_alias};
return mymodule_get_tarifa_final($cliente_nid, $pieza, $descuento, $factor, $cantidad);
gkelly’s picture

I was looking for something else views-related and came across this thread. I'm implementing this now to save myself headaches later. Thanks for saving me from a problem I didn't even know I had yet. Much appreciated!

alliax’s picture

akobashikawa's solution works and I thank you for this, seriously! I was having a field alias change in a same view when the argument was set to all and to something specific, real headache!
Now solved, thanks! Should be mentionned in the custom field module help message so that this problem doesn't happen to everyone one by one..

druplicate’s picture

But what if the variable is an array?

I'm using a Hierarchical Select field with this Views field name: field_neighborhood_hs_value. The field has two values, city and neighborhood, using Content Taxonomy.

I put a DPM($data) in the view and it returned this:

node_data_field_neighborhood_hs_field_neighborhood_hs_value (Array, 2 elements)
    0 (Array, 1 element)
        value (String, 4 characters ) 2189
    1 (Array, 1 element)
        value (String, 4 characters ) 2186

But blindly extending the method in #4, fails:

<?php $x = $data->{$this->view->field['field_neighborhood_hs_value'][0]['value']->field_alias}; ?>

with this error message:

Fatal error: Cannot use object of type content_handler_field_multiple as array in /usr/local/www/apache22/data/sites/all/modules/views_customfield/includes/views_customfield_handler_field_phpcode.inc(118) : eval()'d code on line 6

In addition, the value stored is a taxonomy ID (Content Taxonomy) and what I really want is the taxonomy name, which if I were doing this in a views template, would be: <?php $fields['field_neighborhood_hs_value']->content; ?>

The whole purpose of this snippet is to alter the text displayed in the pager for Views Slideshow, which takes values for its "breakout" fields, from Views fields. In this case I am using some logic to combine two other fields and present it as one field to Views Slideshow using Custom Field.

It doesn't seem possible to accomplish this at the theme level, as Views Slideshow is a Views display plugin and there is no intermediate template in which I can alter variables, unless I'm missing something.

Since the view will never change, the filters are static, and there are no arguments, I'm not worried about changing aliases. Still I can't figure out how to write the object->property correctly. Nothing works when the variable is an array.

druplicate’s picture

This will return the term ID:

<?php $x = $data->node_data_field_neighborhood_hs_field_neighborhood_hs_value[0]['value'];?>

But I couldn't get around the alias issue using the technique in #4.

In order to get the term name, I would have to do a node load and I think that's a bit much for a snippet.

I then tried combining fields using Computed Fields in the node form, but that also failed because the $node object does not contain the ['view'] attribute which holds the term name, until the node display is rendered.

The proper way to do this is to override the theme function for the breakout teaser in the Views Slideshow Thumbnail Hover module.

But still, I'd like to know if it's possible to use the technique in #4 with arrays.

druplicate’s picture

Double clutched - deleted duplicate comment.