Trevor Newhook
Trevor Newhook

Reputation: 909

Drupal User Object

I'm trying to create a custom user profile in Drupal 7. One of the fields references the user object, and I can't figure out what the syntax is to do that. I've read that '->' should be used, but I'm not sure exactly how.

The structure of a print_r($user_object) is at: http://pastebin.mozilla.org/1741565

I'm trying to get to the data inside field_country[data] - just not sure how to do it.

In addition, when I try to access $user_object[user_relationships_ui], nothing is visible. It doesn't throw an error, but doesn't print anything, either.

Thanks for the help

Upvotes: 0

Views: 208

Answers (2)

Trevor Newhook
Trevor Newhook

Reputation: 909

The way I figured out how to do it was to cast the object as an array -

$user_object= (array) $user_profile['field_country']['#object'];

After that, I could access it as a normal array.

Upvotes: 0

Prasanth
Prasanth

Reputation: 5258

Attributes of an object in php must be accessed by ->property_name. And, when using associated arrays, using ['key_name'].

Here, $user_object[0]['user_relationship_ui']['#type'] gives you the #type.

Don't confuse objects with associated arrays.

EDIT: Further explaining,

and see line 18, 19:

[field_country] => Array (
                    [#theme] => field 

see line 31, 32:

[#object] => stdClass Object (
                            [uid] => 1 

The difference is if you want to access #theme, you have to do it by using ['#theme']. And if you want to access uid, you have to do ['#object']->uid.

Upvotes: -1

Related Questions