I have a question. Based on the Drupal 8 Example "content_entity_example", how I can filter all contents/entities so a "normal" user only see and can CRUD his own entities?
I search and search but nothing found. I also try to find other modules for 8.x that needs the same logic (e.g. private message on a community) but i don't found everything.

A short example would by very nice or a module that include this logic/function.

Thanks in advance :-)

Comments

whiterose83’s picture

Now I have a solution, but I'm not sure if it is the best way.

I override in ContactListBuilder.php the methode getEntityIds() with the following:

protected function getEntityIds()
{
    $user = \Drupal::currentUser();

    if(in_array('administrator', $user->getRoles()))
    {
      $query = $this
          ->getStorage()
          ->getQuery()
          ->sort($this->entityType->getKey('id'));
    }
    else 
    {
      $query = $this
          ->getStorage()
          ->getQuery()
          ->condition('user_id', $user->id() )
          ->sort($this->entityType->getKey('id'));
    }

    if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query->execute();
  }