0

I wonder if something "core" already exists that gives me the raw value of a field inside a node. For that matter I wrote this little helper, but it seems too strange not to be able to do it with a core function:

function mymodule_get_node_field($node, $name, $lang = 'und', $key = 'value') {

  if (substr($name, 0, 6) != 'field_') {
    $name = "field_{$name}";
  }

  if (!is_object($node) || !isset($node->{$name}) || !isset($node->{$name}[$lang])) {
    return null;
  }

  return $node->{$name}[$lang][0][$key];
}
1

The only thing I know is field_get_items().

Your function makes a lot of assumptions that don't work for a core function.

  • Not all fields are prefixed with field_, only those created through the UI. body for example not.

  • Some fields have multiple values, your function only returns the first.

If you want a function that makes these assumptions, you could write a simple wrapper for field_get_items() that takes care of that.

  • yeah, I know :) This is just a quick and dirty hack that will work on my fields, in my module. I'd not use that as a generic helper to access any fields, thus my question indeed ;) Off to write a wrapper for field_get_items then – Claudio May 28 '11 at 9:14
  • 1
    field_get_items (and a little cute wrapper) is the answer. – Claudio May 28 '11 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.