Hello !
Thanks a lot for great module !

content type
I have a content type which has 2 fields:
-- price
-- disc1 //discount no.1

views
I create a "node" type view and add the following fields to it:

  • Node: Title
  • Content: price
  • Content: disc1
  • Customfield: PHP code

The value of the Custom field is: print_r($data);

When I press "preview" button i receive the following value for the Customfield:
stdClass Object ( [nid] => 1 [node_title] => product1 [node_data_field_price_field_price_value] => 100.00 [node_type] => test_content_type [node_vid] => 1 [node_data_field_price_field_disc1_value] => 10.00 )

Now, i will remove the "Price" field from the view. Here's the new output for the Customfield:
stdClass Object ( [nid] => 1 [node_title] => product1 [node_data_field_disc1_field_disc1_value] => 10.00 [node_type] => test_content_type [node_vid] => 1)

Please look to the attached screenshot to see more details.

Ass you see, when i remove a field from the view, the name of the remaining fields can (may) change.
Thus it's very hard to create calculations using Customfield, because we will have to verify and update our formulas each time when the new field is added to the view.


Information about my config:
drupal 6.15 (fresh install)
views 6.x-2.8 (the same error if we use latest dev)
cck 6.x-2.6
views custom field 6.x-1.x-dev

Once again thank you for this module. It makes my drupal life easier.
Excuse me for my english.

Comments

intyms’s picture

Title: field names are changing as soon as you add/remove a new field to the view » cck field aliases get renamed when we reorder them

if we reorder the cck fields in the view, their aliases will be renamed.
So when we use print_r($data); inside of the customfield we get the Field Aliases which will change once we reorder the fields.
here is a discussion which may help to find a solution:
#546294: cck fields get renamed according to field order
Can somebody help to implement that solution into Views Custom Field?

intyms’s picture

Title: cck field aliases get renamed when we reorder them » use of the "field id" instead of field aliases
Category: bug » feature
Priority: Critical » Normal

It seems to me that the "field alias" is set by Views and not by Views Custom Field. We can see this by looking to the Query (shown to the bottom of any view).

Is it possible to receive the "field id" or something else (which can remain unchanged once it has been added to the views) instead of Field Aliases?

Thank you.

intyms’s picture

does anyone encounter the same problem?

intyms’s picture

#361756: Updating views fields changes field alias names - can someone implement the solution from here?

infojunkie’s picture

I dont think it's a good idea to use the $view->field['field_host_value']->render($view->result[$count]); approach, since we're interested in the raw value rather than the printable version. The "field id" does not have a strong meaning in the context of a result row, since the SQL query can join several tables which can potentially have the same field name but with different data (when we're not joining on that field).

It's unfortunate that Views should behave the way it does, but a proper fix should go there. Can you suggest a consistent and generic solution that Views Custom Field can implement?

intyms’s picture

Thanks for reply.
No, i don't have any solution.

I am just a user with some "very basic" php knowledge.
The problem is that the "cck field alias" may change when a new "cck field" is added to the view or when we rearrange the "cck fields" inside of the view.

Here is what i do to diminish the impact of this problem.
I place the following code inside of every "PHP custom field" to verify the existence of the fields.

<?php 
//check fields
if (
(!array_key_exists('node_data_field_myfield1_field_myfield1_value', $data))
||
(!array_key_exists('node_data_field_myfield2_field_myfield2_value', $data))
||
(!array_key_exists('node_data_field_myfield2_field_myfield3_value', $data))
||
(!array_key_exists('node_data_field_myfield2_field_myfield4_value', $data))
)
drupal_set_message("Attention, the field alias has changed, please update your view",'error',$repeat = FALSE);
// end of check fields
?>

Then, if the Field Alias changes, Drupal will show me an "error" message. And i will have to go to views and update the fields aliases.

infojunkie’s picture

It's a problem for sure. I'll think of a way to fix this.

intyms’s picture

Thanks a lot !

LEternity’s picture

subscribe

vatavale’s picture

(subscribe)

jerome.megel’s picture

subscribe

drubb’s picture

It's really ugly. Field names should never be changed dynamically. At the moment I'm using this workaround, maybe it helps someone:

In PHP Customfield I'm loading the complete node and thus get access to the "real" CCK field names. For example with a CCK node reference field I do something like this:

$record = node_load ($data->nid);
$field = $record->field_xyz[0]['nid'];
$refer = node_load ($field);
...

This might be expensive for complex nodes, but it works.

Regards,
Boris

narrowstrife’s picture

subscribe

twooten’s picture

I've also just discovered this issue this morning. I'll follow the discussion.

zjk’s picture

same issue, views change my query aliasing everytime...

Begun’s picture

I also encountered this issue and have been looking for a solution. I am working within a views-view-field template preprocess function. The solution I have implemented is to dynamically get the field alias from "$view->field['FIELD_NAME']->alias" and then get the value from $view->result[] using the alias.

$str = 'field_public_value';
$field_alias = $vars['view']->field[$str]->field_alias;
$result_num = $vars['view']->row_index; // Sets the row number so we can get the correct value out of $view->result[]
$public = $vars['view']->result[$result_num]->$field_alias;

I have no idea how robust this is, as I don't know anything about how the object $view is created and the relationships between the values/varaibles stored within it. I am sure there is something fundamentally wrong with my solution, otherwise everyone else would be doing the same thing. Would appreciate it if someone could tell me what is wrong with doing it my way.

On another note, I thought the following was meant to solve this kind of problem, but I have never been able to get it to work.

<?php
// current row field value
$row_value = $row->{$view->field['FIELD_ID']->field_alias};

// current rendered field value (with prefix, suffix et.)
$rendered_value = $view->field['FIELD_ID']->render($row);
?>
restyler’s picture

I'm shocked that $data aliases issue is not even mentioned on views custom field module project page. It should be highlighted with bright yellow background.

dkingofpa’s picture

Agreed. It would be nice to have a warning and explanation on the project page. This is a serious issue. I'm now re-evaluating my use of this module.

casey’s picture

In the D7 successor I print variable names in the config form: http://drupal.org/project/views_php

akobashikawa’s picture

alliax’s picture

akobashikawa's solution works and I thank you for this, seriously!

apaderno’s picture

Status: Active » Closed (outdated)

I am closing this issue, since it's for a Drupal version not supported.