If you have two different CCK content types and you want to reference information in one from the other, one way of doing that is to setup a nodereference field (in this example, field_application) that points to the related node.

Then create a computed field with the following:

<?php
$node_field[0]['value'] = db_result(db_query("SELECT field_phonenumber_value FROM content_type_content_enrollment_application WHERE nid=%d",$node->field_application[0][nid]));
?>

In this example, field_phonenumber_value is the name of the field from the other content type as stored in the database table for the other content type (table content_type_content_enrollment_application). You'll only have to modify the sql slightly to pull a field not tied to a single content type, like a table such as content_field_phonenumber.

Store the result in the database and set the type and size to match the type and size of the original field.

Why would you want to do this instead of just using nodereference to show the contents of the referenced node? Well, if you're creating a view, you may not want to make the view load every single node of the other content type when all you really want is to display the contents of one field.

Comments

KarlM’s picture

Hi

Look here:
http://drupal.org/node/298951

Cheers
Karl

zeno129’s picture

I found a simpler way of doing it if you have a nodereference field:

Suppose you have 2 nodes; node1 has the field you want and node2 has a nodereference field pointing to node1.


$nid = $node->field_from_node2_nodereference[0]['nid'];
$myNode = node_load($nid);
$node_field[0]['value'] = $myNode->field_from_node1[0]['value'];

:)

vthirteen’s picture

i'm not able to use this code and i'm trying to see what i'm doing wrong.

i suppose that "field_from_node2_nodereference" is the name of the nodereference field in node2, right?

what about the display format?
is $display = $node_field_item['value']; correct?

as for data type i have a text field ("text" or "longtext"?) and in another case i have a date field (is it an integer?).

as for the length, i set the same max length of the original text field in the computed field, is that correct? what about a date, should i set a max length too?

harking’s picture

You might not want this as it performs _many_ queries where the example from the snippet only performs one query

jschumann’s picture

Or you might want it because it doesn't touch the database directly. Using hard-coded SQL queries instead of proper Drupal objects should probably be reserved for special teams.

ah000’s picture

Please help.
I have two content type A and B.
A uses Node Reference to reference B.
now I'm creating a View to list all A nodes that have a nodereference to B.
now as far as I know, the nodereference returns the node's title (B's Title), so the question is how do I make it return the node's auther user.name (B's author) instead of the node's title (B's Title) ?

I hope I explained it just fine, please let me know if I should make the question more clear.

Any help would be much appreciated,

Thanks,

NickWebman’s picture

Anyone know what the snippet would be for drupal7?

Anonymous’s picture

I'm also very interested in how this would work in Drupal 7. Any assistance would be greatly appreciated!

ah0’s picture

any help for how the same is done in Drupal 7?
thanks

joseto’s picture