14 March 2011

How to get profile fields in Drupal 7. Profile fields are made with Profile2 module.

About Profile2 Module

Profile2 module is designed to be the successor of the core profile module, which is deprecated for Drupal 7. In contrast to the deprecated module this module provides a new, fieldable 'profile' entity - leverage the power of fields!

You can create profile field groups and give roles permissions to own those groups.

Display the fields

Source code viewer
  1. // Current user information - needed for id.
  2. global $user;
  3. // Define pid(profile id) variable.
  4. static $pid;
  5. // Query to get pid.
  6. $result = db_query('select pid from profile where uid = '.$user->uid);
  7. // Get pid.
  8. foreach ($result as $record) {
  9. $pid = $record->pid;
  10. }
  11.  
  12. // Load profile fields.
  13. $profile = profile2_load($pid);
  14. // Print profile array.
  15. print_r($profile);
  16.  
  17. // OR
  18.  
  19. global $user;
  20. $profile2 = profile2_load_by_user($user);
  21. $profile = $profile2['main'];
  22. // Pretty print profile object using devel module.
  23. dpm($profile);
Programming Language: PHP