Score:0

Load node entities by owner

ht flag

I want to load entities by their owner. This is what I have so far:

\Drupal::logger('migrate_users')->notice("Hello start");
$userStorage = \Drupal::entityTypeManager()->getStorage('user');

$query = $userStorage->getQuery();
$uids = $query
  ->condition('status', '1')
  ->condition('type', 'student')
  ->execute();

$users = $userStorage->loadMultiple($uids);

// help regarding this line
$entities = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadByProperties([
      'type' => 'article',
      'user' => SOME_USER,
    ]);

\Drupal::logger('dssi_migrate_users')->notice("done");

What do I need to pass in for SOME_USER?

Score:2
de flag

You are only using the wrong property name for passing the user ID.

The following would work:

$entities = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadByProperties([
      'type' => 'article',
      'uid' => SOME_USER_ID,
    ]);

If you wish to fetch entities from several users, just use an array:

$entities = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadByProperties([
      'type' => 'article',
      'uid' => [uid1, uid2, uid3],
    ]);

loadByProperties() method use buildPropertyQuery() to pass the values to the query, which use a "IN" condition.

Score:0
cn flag

What you are looking for is IN operation and I think loadByProperties method only acts on Equals operation so, use getQuery method instead and you can attach as many conditions as you want, like below:

$nodeQuery = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
$entities = $nodeQuery->condition('bundle', 'article')->condition('uid', $uids, 'IN')->execute();
misterdidi avatar
de flag
your solution is right but I think it is unnecessary in that case. According to the docs, [loadByProperties()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityStorageBase.php/function/EntityStorageBase%3A%3AloadByProperties/9.3.x) uses buildPropertyQuery() method, which uses `IN` to build its conditions.
I sit in a Tesla and translated this thread with Ai:

TH: SOME_USER
RO: SOME_USER
RU: SOME_USER
VI: SOME_USER
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.