3

In a template for a content type I am loading a node from a node reference.

It loads and if I do a print_r I get this:

stdClass Object ( 
  [vid] => 40 
  [uid] => 14 
  [title] => Cover 
  [log] => 
  [status] => 1 
  [comment] => 0 
  [promote] => 1 
  [sticky] => 0 
  [nid] => 40 
  [type] => portfolio_image_main 
  [language] => en 
  [created] => 1309382711 
  [changed] => 1309382711 
  [tnid] => 0 
  [translate] => 0 
  [revision_timestamp] => 1309382711 
  [revision_uid] => 14 
  [field_portolio_image] => Array ( 
    [en] => Array ( 
      [0] => Array ( 
        [fid] => 5626 
        [alt] => 
        [title] => 
        [uid] => 14 
        [filename] => Cover.jpg 
        [uri] => public://Cover.jpg 
        [filemime] => image/jpeg 
        [filesize] => 147898 
        [status] => 1 
        [timestamp] => 1309382711 
      ) 
    ) 
  ) 
  [name] => jojo 
  [picture] => 0 
  [data] => a:1:{s:7:"contact";i:1;} 
) 

and Im trying to access the single variable here:

$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];

but so far nothing. Any thoughts?

Laxman13
  • 5,226
  • 3
  • 23
  • 27

2 Answers2

2

please try to use below code

$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename]; 
Taryn
  • 242,637
  • 56
  • 362
  • 405
Drupalrk
  • 117
  • 5
1

There is a helper function to access field items for the user's correct language (otherwise, you'd have to hardcode the ['en'] part).

field_get_items()

So your code would end up being something like this:

$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
  print $field_instance['filepath'];
}
theunraveler
  • 3,254
  • 20
  • 19
  • Thanks! changed the code to this: $field_instances = field_get_items('node', $newImage1, 'field_portfolio_image'); // $field_instances should now be an array. foreach ($field_instances as $field_instance) { print $field_instance['filename']; } so it would stop throwing errors, but still will not print the value. – doctorstrange Jul 05 '11 at 02:37
  • If you have the devel module (which I would very much suggest), you can use the `krumo` function. So `print $field_instance['filepath']` would become `krumo($field_instance['filepath'])`. This is much more reliable, since you sometimes do not know if something is going to `print` (i.e. if it's in a function that returns something other than templating language). – theunraveler Jul 07 '11 at 00:14