0

I have created a content type with machine name user_points and let's say have the following fields for this content type:

nid,
title,
reference_id (text),
user (reference of user entity),
points (integer)

the following code give me the sum of all points.

$storage = $this->entityTypeManager()->getStorage('node'); 
    $total_points = $storage->getAggregateQuery()
      ->condition('status', 1)
      ->aggregate('points', 'SUM')
      ->condition('type', 'user_points')
      ->execute();

I would like to add a condition to get total points for specific referenced user by user ID. How can I do it?

2 Answers 2

1

if the machine name of user entity referenced in user_points content type is for example field_my_user, the following condition can be added to the query above to select only nodes with referenced user equal to $user_id

->condition('field_my_user.entity:user.uid', $user_id)
0

The user id for nodes is stored in node_field_data and/or node_revision depending on some settings in your content_type. Join these tables on nid/vid and add a condition for your uid/revision_uid.

1
  • I don't look for user id of user that created the node but for user id of user that referenced in the node.
    – Igal
    Commented May 20, 2017 at 13:07

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.