0

i'm getting a notice

Error message - Trying to get property of non-object

here's my function:

<?php
function func()
{
        $node = menu_get_object();
        global $user;
        if ($node->type == 'hall' &&
            in_array('instructor', array_values($user->roles)))
        {
            return " hall-instructor ";
        }
}
?>
1

You need to check what node_get_object() returns since it could not return an object. This is particularly true when the user is not watching a node page, but even in the case the viewed page is a node one, the function could not return an object. This is why Drupal code always check the returned value.

function book_page_alter(&$page) {
  if (($node = menu_get_object()) && !empty($node->book['bid'])) {
    $active_menus = menu_get_active_menu_names();
    $active_menus[] = $node->book['menu_name'];
    menu_set_active_menu_names($active_menus);
  }
}
// template_preprocess_page()
if ($node = menu_get_object()) {
  $variables['node'] = $node;
}
// template_preprocess_html()
// If on an individual node page, add the node type to body classes.
if ($node = menu_get_object()) {
  $variables['classes_array'][] = drupal_html_class('node-type-' . $node->type);
}
0

You are not preparing node object & trying to access that. Your are using menu_get_object(), which returns node ID only. So something like this should work -

<?php
function func()
{
     $nid = menu_get_object();
     $node = node_load($nid);
     global $user;
     if ($node->type == 'hall' && in_array('instructor', array_values($user->roles))) {
         return " hall-instructor ";
     }
}
?>
  • 1
    No, menu_get_object() returns a stdClass object, when called for the right menu item. – kiamlaluno Aug 2 '15 at 16:15

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.