TransWikia.com

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:

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

Answered by Jayaram on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP