Things are too complicated with Drupal 8 for non-professionals...

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)

I want to get a sum of points for specific user ID for all published contents of type user_points. What is the correct way to do it in Drupal 8?

Comments

Igal’s picture

I found how I can sum the points of all published nodes of content type user_points

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

still have a problem to add a condition that will sum only nodes with referenced user with specific user ID.
->condition('user:user.uid', $user_id, '=')