I would like to be able to have a token for a field in a referenced node. This will be used to set titles, name file names for images and also to be able to use the token in Pathauto. This is my setup:

I have the content type Event (machine name 'event'). In this content type I have a field for a date 'field_time' and a Content Taxonomy for type of event 'event_type'.
I created a new content type Gallery with the node reference 'field_event_reference' to an event.

I use Custom Tokens to create a custom token and this is my custom token to be able to catch the title of the referenced node:
Token ID: token_custom_referenced_time
Type: Node
PHP replacement:

$noderefnode = node_load($field_event_reference[0][nid]);
print $noderefnode->field_time[0]['value'];

But this gives me an empty custom token! Doesn't Custom Token support referenced fields?
I have used Content Templates and output the code above and then it works, but I want the values as a token.

In Gallery I use Automatic Nodetitles and set the title to "[field_event_reference-title] [token_custom_referenced_time]".

The values are in the table "content_field_time" and has values like "2010-10-24 13:30:00". I only want the date from this value, but that is a separat issue.

All help are greatly appreciated!

Comments

magnus’s picture

Status: Active » Fixed

I solved it through this code:

$result = '';

if ($node->field_event_reference) {
  $refnid = $node->field_event_reference[0]['nid'];
  $refnode = node_load($refnid);
  if ($refnode && $refnode->field_time) {
    $date = $refnode->field_time[0]['value'];
    $result = strtotime($date);
    if ($result) {
      $result = format_date($result, 'custom', 'Y-m-d');
    }
  }
  
}

return $result;

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Thank you for this, Magnus.

I used your code and I got something done that has been troubling me.

I needed to pull the 4 digit year from a date_field in a referenced node in order to build the automatic URL alias.

using your code I came up with this:

$result = '';

  $refnid = $node->field_parent_event[0]['nid'];
  $refnode = node_load($refnid);
  $date = $refnode->field_event_center_date[0]['value'];
  $result = strtotime($date);
  $result = format_date($result, 'custom', 'Y');
  
return $result;

It's not exactly the same, and I didn't use any if statements- but so far it doesn't seem to be broken.
Thanks a lot.