I am trying to access a custom node field using _preprocess_html but many of the approaches I've seen on StackOverflow don't seem to work correctly and I don't quite understand why.
I have a checkbox field called 'public' which is available on a node as 'field_public'. I want to find out if it is checked or unchecked in a preprocess_html template function.
If I am not logged into the site and do the following :
function MYTHEME_preprocess_html() {
$node = menu_get_object();
}
Then my field value is $node->field_public[LANGUAGE_NONE][0]['value']
as I would expect. However if I am logged in it changes to $node->field_public[0]['value']
.
Now I could just do
$public = ($node->field_public[LANGUAGE_NONE][0]['value'] || $node->field_public[0]['value'])
But I want to it correctly, and none of the options I have tried seem to give me a value in both cases. I have tried :
// node_build_content method
$node = node_load($node->nid);
node_build_content($node);
$public = $node->content['field_public'];
// entity api method
$wrapper = entity_metadata_wrapper('node', $node);
$public = $wrapper->field_public->value();
// field_get_item method
$public = field_get_item('node',$node,'field_public');
In all these cases the value is fetched when the langauage attribute is present (not logged in) and isn't fetched when I am logged in as admin (when the node object has no language attribute), so I have to fall back on checking two ways. What am I doing wrong?