Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am still learning a lot with Drupal 7 and unfortunately on the fly. I used 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? I have listed the code below. Any help is greatly appreciated as I am still new to this and learning.

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']);

share|improve this question
global $user is always available if you want to access the fields from the logged in user. – Jayaram Apr 26 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 at 17:21

1 Answer

Use the $user 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:

<?php
global $user;
$account = user_load($user->uid);
$entity_field[0]['value'] = $account->field_iweight['und'][0]['value'] - $entity->field_current_weight['und'][0]['value'];
?>
share|improve this answer
Wow thank you so much. :) – user17021 Apr 26 at 17:45
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 at 18:22
Did you use PHP input format ? can you paste the entire code you used ? – Jayaram Apr 26 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 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 at 18:45
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.