I am using a computed field and I want to display the field_first_name and field_last_name, which are properties of the user that created that node.

I have been trying things, but it seems like $entity does not have a uid?

I have no idea how to get load the user. These are the variables I have:

The variables available to your code include: &$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items. To set the value of the field, set $entity_field[0]['value']. For multi-value computed fields continue with $entity_field[1]['value']. Here's a simple example which sets the computed field's value to the value of the sum of the number fields (field_a and field_b) in a node entity: $entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_a'))) + array_pop(array_pop(field_get_items($entity_type, $entity, 'field_b'))); The first pop fetches the last (or only) item from the field while the second pop fetches its ['value'] contents (assuming it's the only key that's set).


Not 100% clear what your use case is, but it's possible that the realname module will do exactly what you're after https://www.drupal.org/project/realname

under /admin/config/people/realname enter tokens for the fields you want eg [user:field_profile_first_name] [user:field_profile_last_name] then whenever the username gets printed it will display these fields instead

| improve this answer | |

Solved it.

$uid = $entity->uid;  // Grab the author of the node (this is a profile-type node)
$user = user_load($uid);  // Load the user object, based on user id
$first = $user->field_first_name['und']['0']['value'];
$last = $user->field_last_name['und']['0']['value'];

$entity_field[0]['value'] = $first." ".$last;
| improve this answer | |

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.