2

I need to have a field with user's picture URL. I'm trying to build it with a Views PHP field.

The field's value is defined

$account = user_load($row->uid);
$uri = $account->picture->uri;
$url = image_style_url('avatar', $uri);
return $url; 

It returns value I expect, but returns also a warning

Notice: Trying to get property of non-object in __lambda_func() (line 2 of /home/testwww/web/informator/sites/all/modules/views_php/plugins/views/views_php_handler_field.inc (131): runtime-created function).

Do you have any idea on how to solve the issue or what exactly is the cause?

From what I've found out:

  • it seems that the notice concerns uri attribute of $account->picture..
  • The result of print_r($account->picture) is
        stdClass Object
        (
            [fid] => 168
            [uid] => 58
            [filename] => picture-58-1310836862.jpg
            [uri] => public://portrait/picture-58-1310836862.jpg
            [filemime] => image/jpeg
            [filesize] => 49505
            [status] => 1
            [timestamp] => 1310836862
            [rdf_mapping] => Array
                (
                )
        )

1 Answer 1

4

Users who have not uploaded a picture (also the anonymous user) will not have any object set for $account->picture. You just need to check that there is an object available:

$uri = isset($account->picture->uri) ? $account->picture->uri : NULL;

image_style_url() will return a bogus URI when passed NULL. You'll want to handle this somehow.

Note:
The user picture is already available as a field, you don't need PHP to get it.

In views 3, you must first add the "Content: Author" relationship before the User fields (including one for the user picture) will show up.

3
  • I need user picture URL, not the picture itself. Unless there's a way to extract the URL from the picture field. Aug 15, 2011 at 11:02
  • I've edited my answer accordingly Aug 15, 2011 at 12:10
  • Hey, how could I have forgotten about users without picture? Spending too much time on one thing makes my mind a little dumb. Thanks a lot for the tip! Aug 15, 2011 at 12:33

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.