0

I'm very close to getting this working right. I created a custom content type with several decimal fields and an entity reference field where you select a user to assign the content to. I am using a computed field to add up the decimal fields and would like to multiply them by a field that I've added to the User profile.

The problem is that I don't know how to reference the field.

This doesn't seem to work:

field_get_items($entity_type, $entity, 'field_percentage_split')[0]

I'm using Drupal 7. Any ideas?

Thank you!

1 Answer 1

1

You should expand the code in your question a little.

First you need to get the assigned uid with something like:

$assigned_uid = $node->field_assigned_user[LANGUAGE_NONE][0]['target_id'];

Then you need to load the user object:

$assigned_user = user_load($assigned_uid);

Then you have access to the value:

$percentage_split = $user->field_percentage_split[LANGUAGE_NONE][0]['value'];

To help visualize the $node and $user objects and how to access the information, you can put the print_r() function into a php-enabled block in your footer.

EDIT:

I looked at your code, and you should not try to do everything in a single statement. No wonder you are having problems. Break it out step by step like this:

// Get the assigned User ID from the node object

$assigned_uid = $node->field_assigned_user[LANGUAGE_NONE][0]['uid'];
// Load the user object for the assigned User.
$assigned_user = user_load($assigned_uid);
// Get the Percentage Split from the user object for the Assigned User (default to 0).
$percentage_split = (isset($assigned_user->field_percentage_split[LANGUAGE_NONE][0]['value'])) ? $assigned_user->field_percentage_split[LANGUAGE_NONE][0]['value'] : 0;
// Get the remaining values from the node object, defaulting to 0 if not there.
$other_ins = (isset($node->field_other_ins_[LANGUAGE_NONE][0]['value'])) ? $node->field_other_ins_[LANGUAGE_NONE][0]['value'] : 0;
$vb = (isset($node->field_vb[LANGUAGE_NONE][0]['value'])) ? $node->field_vb[LANGUAGE_NONE][0]['value'] : 0;
$finder_s_fee = (isset($node->field_finder_s_fee[LANGUAGE_NONE][0]['value'])) ? $node->field_finder_s_fee[LANGUAGE_NONE][0]['value'] : 0;
$broker_fee = (isset($node->field_broker_fee[LANGUAGE_NONE][0]['value'])) ? $node->field_broker_fee[LANGUAGE_NONE][0]['value'] : 0;
$dlc_franchise_fee_hst = (isset($node->field_dlc_franchise_fee_hst[LANGUAGE_NONE][0]['value'])) ? $node->field_dlc_franchise_fee_hst[LANGUAGE_NONE][0]['value'] : 0;

// Do the math.

$value = ($other_ins + $vb + $finder_s_fee + $broker_fee - $dlc_franchise_fee_hst ) * $percentage_split;

You also seem to be trying to use D6 data structures (won't work in D7), and it will also not work to try to give a function array keys.

10
  • Thank you for the quick reply. I'm definitely doing something wrong, here is my complete calculation. I have all the isset( because not all the fields will have values, it may be just one or a combination. pastebin.com/u1C3tJPZ
    – Ash
    Commented Mar 7, 2013 at 13:24
  • Also, I'm using decimals and am getting an error about only being able to flip strings or integers.
    – Ash
    Commented Mar 7, 2013 at 13:32
  • The function field_get_items() returns an array, and is not itself an array. "field_get_items($entity_type, $entity, 'field_broker_fee')[0]['value']" is not a valid representation of the field value. get the value of every field the same way I showed you how to get the uid.
    – Triskelion
    Commented Mar 7, 2013 at 14:57
  • Thank you for the details. Will this fetch the user I'm referencing in my node? I have an entity reference field that references a user. I'd like to fetch the percentage split field from the user that is being referenced.
    – Ash
    Commented Mar 7, 2013 at 16:56
  • $assigned_uid = $node->field_agent[LANGUAGE_NONE][0]['target_id']; $assigned_user = user_load($assigned_uid); $percentage_split = $user->field_percentage_split[LANGUAGE_NONE][0]['value']; $entity_field[0]['value'] = print_r($percentage_split); That spits out "1.00" regardless of what field I reference from the user profile.
    – Ash
    Commented Mar 7, 2013 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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