Hi, I'm currently working with drupal 7 and hope to have a site up and running sometime around after launch.
I'm currently experimenting with theming some node types and want to access the raw value (as stored in the database) of a field (added using the new fields feature) from a node.

Currently, I am theming a special node type, and can access the custom field using $content['field_xxx'];

The only problem is that I can only render the field (render($content['field_xxx']);) but cannot access the raw value...
Since its a drop down list, the raw values are 0,1 or 2 and the displayed value is A, B or C.

Said simply, I don't want to load A or B or C but the base values, since A or B or C might be translated in the interface, I cant rely on that. Lastly depending on the value of field_xxx the structure of the node will change, so I have to check that value...

I've searched high and low and even looked at the CCK docs no no end... any pointers?

Comments

espressochino’s picture

Ok, here is the solution.

Assuming you have loaded a node, through node_load(), and a custom field called xxx, here is how you access the first value (assuming you have just one value set).

$node->field_xxx[$language]['0']['value']

The $language variable is so that you access the correct translation/localized value of the field, and "0" accesses the first item in the field (I only need one).

If you are in a node template, e.g xx--node.tpl.php then you can access each variable like so :

$content['field_xxx']['#items']['0']['value']

Again, this is assuming you want to access the 0th value of the field, like the above example.

jcarlson34’s picture

Thank you for posting the solution. It works great! :)

I had originally tried to add field_name_field_xxx but the solution is was looking for was indeed this: $content['field_xxx']['#items']['0']['value']

daniorama’s picture

Hi, I tried that in comment.tpl.php to add a class in comments depending on the value of a listfied but I get a lot of errors. Could anyone tell what code should I type?

I'm not if I should load the node, comments or field. Could you also include also the code to load the needed ones?

Thank you

hemangi.gokhale’s picture

This worked for me! Thanks.

akanik’s picture

This was an extremely helpful post, thanks so much. However, I was wondering how I would go about getting the body field to print with html tags included. I have the devel module installed, and when i go into it, it seems that $content['body']['#items']['0']['value'] should produce body text with html code included (div tags, body tags, etc.), but it does not. It seems to just utilize the html code, not display it.

Any suggestions?

p.s. the text format is filtered with CKEditor installed.

4ph3x2w1n’s picture

I would also like to say that this post has solved my problem. I had a text field with machine name "field_linkhtml" and I wantd to use that field as the URL to which the title of a node teaser would link to. So in my node--content_type.tpl.php I originally had this:

<h2 class="title" <?php print $title_attributes; ?>><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2>

And was able to get everything working by doing this:

<h2 class="title" <?php print $title_attributes; ?>><a href="<?php print $content['field_linkhtml']['#items']['0']['value']; ?>"><?php print $title; ?></a></h2>

I had tried several things before including "$field_linkhtml", "render($field_linkhtml)", "render($content[$field_html])", etc etc, none of this worked. The above solution works perfectly. Thank you.

jack-pl’s picture

$object = entity_metadata_wrapper('node', $node);
$raw_value = $object->field_xxx->value();

http://drupal.org/node/1021556