5

Here is my code:

function THEME_preprocess_block(&$variables) {
    $variables['user_profile'] = menu_get_object('user');
}

I print the field "name" using print $user_profile->name, and I get the following error:

Notice: Trying to get property of non-object in include() (line 71 of SITE\sites\all\themes\parce\block--block--2.tpl.php).

I assumed $user_profile was not an object and I tried print render($user_profile['name']), which returned me:

Fatal error: Cannot use object of type stdClass

What is going on? I checked the Drupal API reference, and menu_get_object() should return me an object.

2 Answers 2

4

You didn't print line numbers of the second error so I don't know if they happen on then same line. Maybe your notice is from something else.

What you could try is to print out the variable using print_r, vardump, debug or use the Devel module which creates some wrapper functions like dpm to print variables with Krumo.

It should be impossible that the same variable is an object and isn't an object at the same time. But it's hard to say more with the info you provided.

Update:
From your comments, I can see why you are getting the errors:

print $user_profile->field_country['und']['0']['value'];

Error >> Notice: Undefined index: und ...

print $user_profile['field_country']['und']['0']['value'];

Fatal >> Cannot use object of type stdClass as array

The problem as I marked in the error message is that und is not defined in the field value. So $user_profile->field_country is ok but $user_profile->field_country['und'] is undefined. The reason could be that it doesn't have a value set or that the language for the field isn't undefined. You should like I suggested above, print out the variable to see what it contains.

4
  • Hi googletrop, thanks for the input, i try to reproduce it again with the line numbers: line 65 > print $user_profile ->field_country['und']['0']['value']; Error >> Notice: Undefined index: und in include() (line 65 of E:\www\parce\sites\all\themes\parce\block--block--2.tpl.php). Now with: line 65 > print $user_profile['field_country']['und']['0']['value']; Error >> Cannot use object of type stdClass as array in E:\www\parce\sites\all\themes\parce\block--block--2.tpl.php on line 65
    – silkAdmin
    Commented Jun 15, 2011 at 7:09
  • 1
    As googletorp suggested, install devel.module and then just do dpm($user_profile); That will tell you the exact type of that variable and the content. Maybe you are not viewing a user/x page? You need to make sure to only display that information when you are on a site where menu_get_object('user') returns anything.
    – Berdir
    Commented Jun 15, 2011 at 7:48
  • @silkAmin Check my updated answer.
    – googletorp
    Commented Jun 15, 2011 at 10:16
  • Yes, thank you, I need to make some more testing but that seems to work.
    – silkAdmin
    Commented Jun 15, 2011 at 15:51
0

menu_get_object('user') returns you an object only for specific URLs; if the URL of page where the block is visible is http://example.com/node/1, then the function will not return you an object.

As reported in the documentation:

menu_get_object() provides access to objects loaded by the current router item. For example, on the page node/%node, the router loads the %node object, and calling menu_get_object() will return that.

In your case, the page URL should match user/%user, such as in the case of http://example.com/user/1, http://example.com/user/1/shortcuts, http://example.com/user/1/devel.

The function could also not return an object when the block is being shown in a user profile page, and if there are problems loading the user object. In this case, the user profile page would not be correctly visualized too, I guess.

3
  • Thank you for your answer but this can't be the case because even though print $user_profile ->field_country['und']['0']['value'] give me an error when i am on the administration side of the site, the output is still correctly printed on the front end, in this instance i get the country name.
    – silkAdmin
    Commented Jun 15, 2011 at 15:36
  • @silkAdmin It depends from the path of the page you are visiting; it's not a matter of back end versus front end. If the administration page is at admin/config/people/accounts, then menu_get_object('user') doesn't return at object.
    – avpaderno
    Commented Jun 15, 2011 at 19:13
  • @silkAdmin In your question you report that the error you get is because you use print $user_profile->name, where $user_profile contains the value returned in from menu_get_object('user'); in that case, the error you see can only be caused from the fact you don't get an object from the function, which can happens as I explained. If that is not the code you used, then you should update your question.
    – avpaderno
    Commented Jun 15, 2011 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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