Is there any function that takes the nid of a node as an argument and returns a reference to that node?

I've searched the net pretty heavily now and I'm not seeing anything but I can't imagine that it doesn't exist.

Comments

j_ten_man’s picture

ELFanatic’s picture

It looks like it should work... hmmm.. What I am trying to do is make a contemplate for one content that also displays fields from another content. But it's not working.

I'm pretty new to php so maybe that's the issue because it should work. This is the line I'm using.

print node_load('150')->content['field_action_date']['field']['items'][0]['#item']['safe'] (note, the $nid will be dynamic later)

But nothing shows. Do you have any ideas as to why it isn't working? Did I explain what I am doing well?

j_ten_man’s picture

You need to put the node in a local variable before trying to get to it's values:

$node = node_load(150);
print $node->content['field_action_date']['field']['items'][0]['#item']['safe'];

Just as a note, I am not sure what you are trying to get from the node, so I can't guarantee that this will print anything but it will not kill your site. At worst it will just print nothing (an empty string).

ELFanatic’s picture

Damn, that didn't work either. That is really weird.

$node->content['field_action_date']['field']['items'][0]['#item']['safe'] is just supposed to produce a field on a content with the field name being field_action_date. It was automatically produced by contemplates so it should work. Getting it to work in a contemplate for a different content some reason isn't work. But I just don't see why not.

jasonnovember’s picture

Hi,

I think I am doing something similar.
I am replacing the custom field of a custom type with values.
Following is the line of code that I use to access the custom field and replace it:

$node->field_policy_body[0]['value'] = token_replace($node->field_policy_body[0]['value'], 'drupalImport');

That is how I accessed the custom field that I had defined.

nevets’s picture

node_load "only" loads the node, it does not prepare it for viewing. The element you are using only occurs after the node is prepared for viewing. You need to call node_build_content().

jasonnovember’s picture

Hi,
Whats the difference between node_object_prepare(&$node); and node)build_content()??

Also, if we do the following,

$node = array('uid' => $user->uid,
		'name' => (isset($user->name) ? $user->name : ''),
		'type' => 'page',
		'language' => ''
	);

node_object_prepare(&$node);
node_save(&$node);

it still created the node.
Just curious :)