Forum Moderators: open

Message Too Old, No Replies

Drupal: getting list of fields, profile type

Working on a custom module, no idea how to solve this

         

explorador

12:55 am on Aug 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi there, this time I had to face this "create a module" on Drupal 7. I have everything working, the client asked for something -sepcial-.

So I have profile types this way I can add custom fields for the profile (user) page. Like age, country, whatever.

Now I need to create a module that will gather this list of fields and it's content. I researched and found:

$users = entity_load('user');
$user_names = array();
foreach ($users as $user_id => $user) {
$user_names[] = $user->name;
}


ok it works, it loads the list of user names, but I need fields from the profile type section. Yes Drupal shows users and profile types as one but they are not together or the same.

Any help will be appreciated. I can do it via Views but it's a module I'm creating right now so i need to get the list of X fields created on the profile type section.

ergophobe

1:57 pm on Aug 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you trying to get the list of fields that are associated with a profile type (e.g. Profile2 profiles) or are you trying to get the values of the fields?

For the list of fields, you'll probably need to use one of the functions from the Field Info API ( [api.drupal.org...] ). I'm not sure which one, because I'm not sure how native profiles use bundles and the entity API in general.

I think you'll want something like

$fields = field_info_instances('user', 'profilename');

or possibly

$fields = field_info_instances('profile', 'profilename');

I don't know how profiles work, so that's just a generic solution. I'm not sure what the parameters would be.

Then once you have your list of fields, you can iterate through them and get whatever info you want with field_get_items() [api.drupal.org] or whatever (that's the "language-safe" way to get values, rather than trying to access field values directly, which is best avoided).

If you need meta data for the field rather than values, you would use something like

field_info_instance($entity_type, $field_name, $bundle_name)

That's all pretty general, but unfortunately I have not worked much with profiles and am not totally sure what you're trying to do.

ergophobe

2:08 pm on Aug 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looking around, the profile fields should get loaded when you do the user_load, so you should be able to just iterate through that.

If you're just looking to get the values, then you would just do a

$user_vals = user_load($uid);
$vals = field_get_items('user', $user, $field_name);

Again, not totally sure how this works with profiles. That's how it would work with nodes though.

explorador

3:34 am on Sep 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you ergophobe for the help and time, I read the thread but didn't update it I was in a hurry. This is what I ended using hope it helps others:

$users = entity_load('user');
foreach ($users as $user_id => $user) {

$profile_main = profile2_load_by_user($user_id, 'main');
$myvar=($profile_main->MACHINE_NAME_OF_FIELD['und'][0]['value']);

}

ergophobe

4:15 pm on Sep 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was thinking you were trying to achieve something else entirely.

entity_load('user')


That's going to load all users on the site as objects. That could get heavy and it might ultimately make sense to build an SQL query and get the data you want with less overhead.

$myvar=($profile_main->MACHINE_NAME_OF_FIELD['und'][0]['value']);


Two things
1. I don't understand the syntax. Why is the $profile_main variable in parentheses? It looks like you're just assigning the last value to $myvar, so why iterate through all the users?

2. Generally you should not access field values directly using 'und' - if the language settings on your site change, that code will break. As I mentioned in the previous, you should us field_get_items().

See
[api.drupal.org...]

And Berdir's comments here
[drupal.stackexchange.com...]

explorador

9:29 pm on Sep 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Ergophobe, I honestly don't understand the details behind that code but it works. Found it hard to get documentation on this and it's kinda cryptic and there are very few results. I will keep an eye on that, thanks.