I need to reference a custom field I created in User Profile

Drupal Answers Asked by user17021 on December 15, 2020

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

One Answer

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

Answered by Jayaram on December 15, 2020

Add your own answers!

Related Questions

File field upload in modal/dialog

1  Asked on December 4, 2020 by ctrnz


How to pre-select items in a select with chosen

1  Asked on December 3, 2020 by jav


Update existing node (not created by Migrate) via Migrate 8.5.x

1  Asked on December 3, 2020 by vaibhav-rana


Secondary Tabs and views

1  Asked on December 2, 2020 by timwhelan


alter Suggestion box for apache solr autosearch

2  Asked on December 1, 2020 by anamika


User specific permission to add links to a menu

2  Asked on November 26, 2020 by mavicc


How to alter sort handler to keep existing order intact in views

1  Asked on November 18, 2020 by hiranya-sarma


Issue using Config Split on content types

0  Asked on November 18, 2020 by vecta


How to create simple confirmation page?

3  Asked on November 11, 2020 by mathewdragon


How do I add a template suggestion for a Views block?

2  Asked on October 21, 2020 by wbeasley


How to programmatically manage taxonomies?

1  Asked on October 15, 2020 by deny-dias


CSS-tags added using Display suite on products disappears

1  Asked on October 4, 2020 by anders-wallenquist


I have issue with commerce_order_load_multiple function

1  Asked on October 1, 2020 by user3686276


Why can’t I install even if I have the required PHP version?

0  Asked on September 13, 2020 by hendrik-kaiser


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir