Closed (fixed)
Project:
Profile 2
Version:
7.x-1.2
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
26 May 2012 at 03:45 UTC
Updated:
30 Oct 2012 at 19:21 UTC
I created a dropdown-menu named title in my profile2-module (in combination with taxonomy) and use it in the registration form.
How can I best determine the user-selected value?
//The following 3 lines of code resulted in the following:
//echo '<pre>';
//print_r($profile);
//echo '</pre>';
[field_title] => Array
(
[und] => Array
(
[0] => Array
(
[tid] => 2
[taxonomy_term] => stdClass Object
(
[tid] => 2
[vid] => 2
[name] => Mr
[description] =>
[format] => plain_text
[weight] => 0
[vocabulary_machine_name] => title
)
)
)
)
The following lines of code work quite well, however there might be a simpler way.
// load profile fields
global $user;
$uid = user_load($user->uid);
//Load 'Personal'-Fields
$profile = profile2_load_by_user($uid, 'personal');
$title = $profile->field_title['und'][0]['tid'];
$sql = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid = :title', array(':title' => $title));
$result = $sql->fetch();
if (!empty($result)) { $title = $result->name; }
Any suggestions?
Thank you!
Comments
Comment #1
Nikdilis commentedAnyone with a better solution?
Comment #2
Nikdilis commentedSolved:
See: http://drupal.org/node/1323842
$title = $profile->field_title['und'][0]['taxonomy_term']->name;
no mysql-query needed.
Comment #3
faqing commentedThank you, Nikdilis
The long coding your provided works great. But the short one does not.
Anything wrong:
$title = $profile->field_title['und'][0]['taxonomy_term']->name;
print $title;
I need you further help:
If I want to display a node reference (link to another node title) not taxonomy term, what I should do?
print ($profile->field_course['und'][0]['nid']);The above code only displays the node number. I want to display the title.
Comment #4
Nikdilis commentedHere's an example of getting values of text-fields and names of taxonomy-fields in Drupal 7.
This is not related to Profile2 but may be helpful nevertheless!!!!
Comment #4.0
Nikdilis commentednone