I used Drupal 7 computed fields to calculate weight loss. This is what I current have. Which works in a sense but only where the user inputs their initial weight and subtracts their current weight to track the total weight loss at the time of the input. But I need it to reference their initial weight from their user profile so that they only have to enter their current weight. One step process versus having to always enter the data that already exists. How can I achieve this?

Current Code which works great but two fields have to be filled in. Needs to reference field in user profile.

$entity_field[0]['value'] = ($entity->field_iweight['und'][0]['value']) - ($entity->field_current_weight_['und'][0]['value']);
  • global $user is always available if you want to access the fields from the logged in user. – Jayaram Apr 26 '13 at 17:12
  • I am sorry to ask how would i put that into the syntax? or I guess query? I am very new to this. I have a little coding experience but it is not strong yet. :) unlike you awesome experts here. – user17021 Apr 26 '13 at 17:21

Use the $user object to access the logged in user.

 global $user;
 $account = user_load($user->uid);

Now you can use $account->field_iweight['und'][0]['value'] (just an example) to access the weight or any other field from the user profile.

Updated code:

global $user;
$account = user_load($user->uid);
$entity_field[0]['value'] = $account->field_iweight['und'][0]['value'] - $entity->field_current_weight['und'][0]['value'];
| improve this answer | |
  • Um ok so I am not sure if I made a mistake doing this. I attempted to configure the computed field per the example above. I hit save but now I am unable to access the dashboard as an admin or regular user. Now I am trying to figure out how to correct this. :) Any thoughts? – user17021 Apr 26 '13 at 18:22
  • Did you use PHP input format ? can you paste the entire code you used ? – Jayaram Apr 26 '13 at 18:28
  • I probably messed this up. Sorry for all the headaches. I appreciate your patience. global $user; $account = user_load($user->uid); $entity_field[0]['value'] = ($account->field_iweight['und'][0]['value']) - ($entity->field_current_weight_['und'][0]['value']); – user17021 Apr 26 '13 at 18:33
  • I updated the code segment in my answer. Is it $entity->field_current_weight_[...] is there an _ at the end of the field name? – Jayaram Apr 26 '13 at 18:45
  • Yeah that is the way it was created. I unintentionally left the extra _ there. Thank you again one question? How do I insert the code if I am unable to access it via the online interface? – user17021 Apr 26 '13 at 18:56

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.