Hello,

I'm noticing some weird behavior with getting the path to an image. I have a field called "field_image" on my nodes.

When I edit a node template, I can get the value (path) simply with this:

$uri = $node->field_image['und'][0]['uri'];

This will give me something like "public://sites/default/files/whatever.jpg." If there is no value, it will default to whatever I have set up in that field configuration and point to "public://sites/default/files/default_images/default-image.jpg".

Perfect!

The problem is this default fallback only seems to be working on the node tpl file. If I try to call nodes from other areas using something like:

$customNode = node_load(1234);

Then I do this:
$uri = $customNode->field_image['und'][0]['uri'];

It won't pull the default image. It will only pull the value if there is an image added to that node. It's almost like the default value won't get set if I'm loading the node with node_load.

Has anyone else came across this? Is there a way I can get the default value through a work around?

Thanks!

Comments

nevets’s picture

After node_load() I would call node_view() which prepares the node for viewing.

dan.freeman’s picture

Thanks so much! This worked perfectly.