I'm needing to get the title of a referenced node from within its parent so I can use autonodetitles module. Since Token module isn't working for fields in D7, I'm trying to do this by pulling fields out of the $node object. Here's the field I'm interested in, at $node->field_cal_event_parent

  [field_cal_event_parent] => Array
       (
           [und] => Array
               (
                   [0] => Array
                       (
                           [nid] => 59      //CAN GET THIS VALUE
                           [access] => 1
                           [node] => stdClass Object
                               (
                                   [nid] => 59
                                   [vid] => 59
                                   [type] => production_external_
                                   [language] => und
                                   [title] => Comedy Club    //CANNOT GET THIS VALUE
                                   [uid] => 1
                                   [status] => 1
                                   [created] => 1307347398
                                   [changed] => 1307348726
                                   [comment] => 1
                                   [promote] => 0
                                   [sticky] => 0
                                   [tnid] => 0
                                   [translate] => 0
                                   [uri] => Array
                                       (
                                           [path] => node/59
                                           [options] => Array
                                               (
                                                   [entity_type] => node
                                                   [entity] => stdClass Object
*RECURSION*
                                               )

                                       )

                               )

                       )

               )

       )

So the problem is that although I can get as far as the referenced node's nid using
$node->field_cal_event_parent['und'][0]['nid'];

I can't find the syntax for going one level deeper and getting the referenced node's title field.

While this code will work, it feels that I shouldn't need to load the referenced node just to get the title field:

$mynid = $node->field_cal_event_parent['und'][0]['nid'];
$mynode = node_load($mynid);
print $mynode->title;

Is there an easier way?

thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Everett Zufelt’s picture

Title: Best way to access title of referenced node within parent $node object » Properly implement hook_token_info()
Version: 7.x-2.0-beta3 » 7.x-2.x-dev
Component: Miscellaneous » Code: node_reference
Category: support » bug

I would also like to use a token to access the node title. I have a field:

'field_name;

I expect to be able to access the referenced node title with the token:

[node:field-name:title]

but that is not working.

With devel and token installed on node/<n>/devel/token the field is showing up as:

[node:field-<referenced-content-type-machine-name>]

I tried:

[node:field-<referenced-content-type-machine-name>]
[node:field-<referenced-content-type-machine-name>:value]
[node:field-<referenced-content-type-machine-name>:title]

Nothing seems to work to get a token to access the node title of a referenced node.
Everett Zufelt’s picture

Category: bug » feature
Status: Active » Needs work
FileSize
1.11 KB

Here is a first pass at node_reference_token_info() It is not working but I thought I'd upload it in case someone else wishes to continue where I left off.

Everett Zufelt’s picture

Will require token module patch at #691078-320: Field tokens

Everett Zufelt’s picture

Status: Needs work » Needs review
FileSize
1.14 KB

This seems to be working in part. I can access the node reference with [node:field-name] You will need the Field Token patch along with this patch.

Everett Zufelt’s picture

Removing tag

andypost’s picture

Status: Needs review » Needs work
+++ b/node_reference/node_reference.moduleundefined
@@ -1053,3 +1053,34 @@ function node_reference_views_options($field_name) {
++      'needs-data' => 'node-reference-field',

What it means? (++)

Powered by Dreditor.

Bevan’s picture

Status: Needs work » Needs review
FileSize
226.92 KB
1.3 KB

The patch makes node-reference fields appear in the Token UI's "Nodes" section. E.g. [node:field-contact] where "contact" is a node-reference field. In addition to this, a new section in the UI called "Node references" is created, which does not make sense in the context of a node-type settings form. This appears to be because of the extraneous plus character ("+") in the patch from comment #4. This character was noted in comment #6.

I removed the extra character from node_references.module and the "Node references" section disappeared.

I am still not sure how the [node:field-contact:field] and [node:field-values:?] tokens and sub-tokenswould be useful or what data they expose.

The attached annotated screenshot notes this same information.

The attached patch is the same as #4 but with the extra plus character removed.

I was able to use this patch to use the title of a referenced node as a token in an automatice nodetitle.

Status: Needs review » Needs work

The last submitted patch, 1192504-7-token_support.patch, failed testing.

emackn’s picture

I think at a bare minimum, it would be a big step to just provide the Id's for the items being referenced. There is a token_function module, http://drupal.org/project/token_function , that you could use then pass those values into and process those into whatever info you needed.

emackn’s picture

Everett,
which field token patch are you referring to?

Anonymous’s picture

Status: Needs work » Needs review

#7: 1192504-7-token_support.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1192504-7-token_support.patch, failed testing.

andypost’s picture

index 46d1e13..5d74f07 100644
--- a/app/sites/all/modules/contrib/references/node_reference/node_reference.module

--- a/app/sites/all/modules/contrib/references/node_reference/node_reference.module
+++ b/app/sites/all/modules/contrib/references/node_reference/node_reference.module

patch should be done from root of module - a/references

andypost’s picture

entity module has entity_tokens sub-module whick mostly works except pathauto (returned url have all '/' are removed)

Related issue #691078: Field tokens

stano.lacko’s picture

Basic token fields are implemented in current token core module, but I need to add token for nid, as argument to Viewfield module of referenced field.

Current sulution [node:field-someone] return plain title of referenced module, but I need to get a NID of referenced module. Current version of Node references doesn't implement token for nid, or value.

So as original post of this issue, current situation is differencent, i dont'n need title but nid (original want title except of nid) ;-)

Anonymous’s picture

Status: Needs work » Closed (works as designed)
andypost’s picture

Status: Closed (works as designed) » Needs work

This module require token support, please don't change status