I'm trying to access a node variable, the id of a taxonomy term but i have to do it at the page level in theme_prepocess_page function. i did it in preprocess node like this

$libStatus = $vars['field_feature_library_status']['und'][0]['tid'];

in preprocess_page i tried this:

$libStatus = $vars['node']['field_feature_library_status']['und'][0]['tid'];

but i get this error... Cannot use object of type stdClass as array in….

can someone explain how i can access this node variable in theme_preprocess_page function?

closed as off-topic by kiamlaluno Sep 1 '14 at 5:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.


Have you tried field_view_values() and field_get_items()

function mytheme_preprocess_page(&$variables) {
    if (!empty($variables['node'])) {
      $output = '';
      $node = $variables['node'];
      $items = field_get_items('node', $node, 'field name');
      foreach ($items as $item) {
        $field_output = field_view_value('node', $node, 'field name', $item);
        $output .= render($field_output);
      // Set $variables with the value you need.

Explained in here how to access node variables in region preprocess function?

I think you should also be able to use entity_metadata_wrapper as follow

$node = $variables['node'];
$wrapper = entity_metadata_wrapper('node',$node); 
$libStatus = $wrapper->field_feature_library_status->value(); 
  • $field_output = field_view_value('node', $node, 'field name', $item); – TopTomato Aug 31 '14 at 19:52
  • but i see it if i do a var_dump($items); – TopTomato Aug 31 '14 at 19:54
  • correction, no error. sorry, had a typo. – TopTomato Aug 31 '14 at 19:56

As reported in node.tpl.php, the field field_feature_library_status is available as raw value as $field_feature_library_status, in the preprocess/process functions.

Field variables: for each field instance attached to the node a corresponding variable is defined; for example, $node->body becomes $body. When needing to access a field's raw values, developers/themers are strongly encouraged to use these variables. Otherwise they will have to explicitly specify the desired field language; for example, $node->body['en'], thus overriding any language negotiation rule that was previously applied.

  • ??? i don't understand – TopTomato Aug 31 '14 at 19:34
  • $field_feature_library_status, just says undefined variable when used in preprocess node – TopTomato Aug 31 '14 at 19:35

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