With computed filed one can make calculations of two or more fields of a node like explained here: http://drupal.org/node/149233
That's clear and I used that already successfully. But how can I use for example values that are in node/1 in a computed field that is used in node/2?

Comments

p6’s picture

Well. You'll need to do the following

  • Set up a CCK Noderef field first, to refer to node(s) that you want
  • Use computed field to grab the nid, load the node and make available that node's fields for computation

You can see something similar here (sort of): http://drupal.org/node/87546

- Pavan Keshavamurthy
http://grahana.net/

- Pavan Keshavamurthy
http://grahana.net/

GiRo’s picture

Thanks a lot. Got it finally working with this code:

$node_field[0]['value'] = db_result(db_query("SELECT field_YOURFIELDNAME_value FROM content_type_YOURCONTENTTYPE WHERE nid=%d",$node->field_YOURNODEREFERENCEFIELDNAME[0][nid])) * $node->field_THISNODESFIELD[0]['value'] ;

Notes:
YOURFIELDNAME is the field name of the other node's field to which the node reference field is referring to. You need to add '_value' to the field name itself as the field itself is stored with that appendix in the MySQL-Table.
YOURCONTENTTYPE is the content type of the node referred to.
YOURNODEREFERENCEFIELDNAME is the field name of the node reference field.
THISNODESFIELD is the field of the node where this computed field is used.

Cheers!

asak’s picture

Quick question:

THISNODESFIELD is the field of the node where this computed field is used.

You mean the name of the computed field this code is in?

I've used that exact code without the last quoted part succesfully, so i wonder what will adding * $node->field_THISNODESFIELD[0]['value'] actually do...?

BTW - would you know how to get the LABEL of the field instead of the KEY (I'm using allowed values like y|Yes n|No etc.)

GiRo’s picture

with $node->field_THISNODESFIELD[0]['value'] I mean any field in the node where the computed field is located.

My question was:
But how can I use for example values that are in node/1 in a computed field that is used in node/2?

So with db_result(db_query("SELECT field_YOURFIELDNAME_value FROM content_type_YOURCONTENTTYPE WHERE nid=%d",$node->field_YOURNODEREFERENCEFIELDNAME[0][nid])) I get a db query done to a field in node/1.

The computed field is in node/2
And with $node->field_THISNODESFIELD[0]['value'] I address simply a field in node/2.
With * I just multiply this two values, you can also do any other operation.

Alpha5’s picture

This code do well if the result is not an array. If the YOURCONTENTTYPE have revision, then will have multi result. Can someone have code to work even if content type reference have revision or not?

sylvaticus’s picture

I'm at the same point.. spent hours to understand why the code was working on some nodes and seems not to work on others.. and I arrived to your conclusions :-)

Did you manage to have a code that extend the query to fetch the latest revision of a node?

Thank you,
Antonello

LEternity’s picture

Unfortunately, this doesn't address the computed field update issue. The field updates only if the node reference field changes. It doesn't react to updates in the field from the external node at all :(.

Suggestions anyone?

harking’s picture

use a rule to save the related record when the original record is saved.

dianacastillo’s picture

Can you detail how you used Rules to save the related record when the orginal record is saved please?

Diana Castillo

peterparker’s picture

I cannot get this to work correctly, please help me to understand if I am using it correctly.
I have two content types, students and hours.
Each student is referenced by many hours.

I already have a view set up which will display inside of the student node (using viewfield). I would like to have the sum of all of the hour nodes appear in a computed field. Is there an easy way to do this?

offbeatlink’s picture

Hi, I want to know how to use this code but in drupal 7, because I got it working on a d6 instalation, but on drupal 7 the code change, the query to the database is not the same, can you help me with that please!
thanks

issues4drupal’s picture

I am a new to drupal.

I dont know where to put this code?

$node_field[0]['value'] = db_result(db_query("SELECT field_YOURFIELDNAME_value FROM content_type_YOURCONTENTTYPE WHERE nid=%d",$node->field_YOURNODEREFERENCEFIELDNAME[0][nid])) * $node->field_THISNODESFIELD[0]['value'] ;
Notes:
YOURFIELDNAME is the field name of the other node's field to which the node reference field is referring to. You need to add '_value' to the field name itself as the field itself is stored with that appendix in the MySQL-Table.
YOURCONTENTTYPE is the content type of the node referred to.
YOURNODEREFERENCEFIELDNAME is the field name of the node reference field.
THISNODESFIELD is the field of the node where this computed field is used.

Tafa’s picture

code goes into your computed field cck created for calculating a value.
T