I have a collection content type which is just a list of EntityReferences to articles.

When I am loading an article, how can I use the Entity API to check if it belongs to one or more collections?

Previously I would query the DB, but I wonder if there is a more idiomatic way?

  • If you do not know all fields that might be referencing this entity type then I doubt you can get that information with entity query. One thing that comes to mind is to load all entity reference fields, filter out those that do not allow your entity to be referenced and then construct entity query accordingly to match those fields and entity types they are attached to.
    – user21641
    Dec 18, 2015 at 15:47
  • Thanks @IvanJaros for your comment. My use case is not that complex, There is only one field that references the articles so far.
    – Esclapes
    Dec 18, 2015 at 19:59

1 Answer 1


To load all collection id's, which have a reference to $article_id:

$collection_ids = \Drupal::entityQuery('node')
  ->condition('type', 'collection')
  ->condition('field_article_ref', $article_id)
if ( count($collection_ids) > 0 ) {
  echo "the article $article_id is referenced in one or more collections";
  • Thanks, it works. Nice and simple.What would be the right way to encapsulate this method? I would like to be able to do something like $article->collections() but that is maybe a different question
    – Esclapes
    Dec 18, 2015 at 19:54

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.