3

How to get all contents from which the current user is author?

 $currentUser = \Drupal::currentUser();
 $user = User::load($currentUser->id());
//get the contents from which $user is author?
3
  • "Content" could mean a lot of things-- nodes? entities? comments? etc. Commented Apr 22, 2021 at 13:19
  • i am interested particularely by nodes and medias , but all entities would do
    – Matoeil
    Commented Apr 22, 2021 at 13:34
  • 4
    There isn't an API for querying across multiple entity types - you need to use an entityQuery (or loadByProperties) for each type you're interested in, with a condition for the author field based on the user id. If you want to write something generic, you can inspect the entity keys to see what field the entity type uses as its owner
    – Clive
    Commented Apr 22, 2021 at 13:54

1 Answer 1

5
$currentUser = \Drupal::currentUser();
$query = \Drupal::entityQuery('node');
$query->condition('uid', $currentUser->id());
$author_nids=$query->execute();

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.