I hoped print $node->field_myfield would work, but only got "array".

Comments

nevets’s picture

Try

<pre>
<?php
print print_r($node->field_myfield, TRUE);
?>
</pre>

to see the elements of field_myfield. I believe you want $node->field_myfield['value'].

WorldFallz’s picture

i can never remember the complete structure of the field array, the best way to see what's available is to do:

<?php
print_r ($node->field_fieldname);
?>

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

clivesj’s picture

You can also install devel module. It gives you all the node values you need.
And by the way: If I'm not mistaken page.tpl.php only carries $node-data when it's a single node page.
Otherwise you will have put your code in node.tpl.php or node-MyContentType.tpl.php

imrook’s picture

The advice given in the previous posts is right on. I'm looking from a different point of view though. What about if the page is a view or an admin page? I think you want to be putting your code in node.tpl.php unless you are already putting proper guards around this code.

helgest’s picture

I don't get any output when trying: print_r ($node->field_filvedlegg['value']). (print_r ($node->field_fieldname) worked just fine)

The reason I don't wont to put this in node-MyContentType.tpl.php is that I want to posision my field at the top of the page, away from the rest of the node.

Btw, I'm all new to Drupal, so maybe there is another easier way.

imrook’s picture

Well, when things get desperate, you can always do print_r($node) and see the whole thing. var_dump($node) makes it look a little nicer, but won't show you the entire structure. It may take a little while to sort it all out, but it can be informative. Usually CCK fields will have ['value'] and ['view']. The value is the raw data and the view is usually formatted for display. You'll probably want to be using the view portion.

If this field really makes sense somewhere outside the main content area, it may make sense to make a custom block and place it in a bock region. But that's only going to be possible after your grasp these concepts anyhow. Perhaps if you gave us more information about your content type and desired page layout, we could offer some advice.

helgest’s picture

Am I using the correct syntax?

print_r ($node->field_myfield) works fine.

WorldFallz’s picture

IIRC, i think it's:

<?php
print_r ($node->field_myfield[0]['value']);
?>

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

aakanksha’s picture

Thanks

kbeck303’s picture

This seemed to work for me
<?php print t($node->field_my_field[0]['view']); ?>

aakanksha’s picture

Thx for ur reply Kathy303Beck,
Its working in page.tpl.php when we are login else it doesn't work.

kbeck303’s picture

@aakanksha - If you are seeing it when you are logged in, you might need to change some of the anonymous user permissions.

Attila’s picture

It's working, but when displaying a node of another node type which doesn't have that field, it just says "Array". Is there a way to remove that?

nevets’s picture

You could try

<?php
if ( !empty($node->field_my_field[0]['view']) ) {
  print $node->field_my_field[0]['view'];
}
?>
Attila’s picture

Works perfectly, thanks a lot!

aschiwi’s picture

You don't need to place that in a t() string. t() makes words inside the brackets translatable strings.

murtza’s picture

this works for me.

<?php
print $node->field_MyField['und'][0]['value'];
?>
buildyourbox’s picture

The above worked for me except I was getting an error;

Notice: Undefined index: und in include() (line 47 of /home/... .../page.tpl.php).

To fix the problem I simply changed the code to;

<?php
if(!empty($node->field_MyField['und'][0]['value'])) {
print $node->field_MyField['und'][0]['value'];
}
?>

This was suggested further above, hope this helps if anyone else gets this error.