According to the documentation for the condition() function, you can append columns to a field name, but I haven't found a way to make a condition on the referenced entity's type.

// Something like this?
$query->andConditionGroup()->condition($referenceFieldName, $someId)
     ->condition("$fieldName.entity:type", ['character', 'movie'], 'IN')

Let's say with the above I am querying for articles that have a reference to the ID in $someId and that reference is to a content type of either character or movie. Is something like this possible with entityQuery?

Background: I'm using the Dynamic Entity Reference module, which allows me to make a reference field that can point to either a taxonomy term or a piece of content. I want to query for references to content in that field, but am concerned that there could be an ID overlap. That is, a taxonomy term and character for example that both have the ID 123. If I am searching for the ID 123, I would only want articles that have references to the characters or movies with the ID 123, not any taxonomy terms with the ID 123.

1 Answer 1


since the reference field is using DER, you can use $fieldName.target_type for finding the referenced entity's type.

  • While it doesn't throw an error about target_type, it also doesn't seem to match anything, either. For example if I use $someId that points to a movie content, it doesn't match target_type set to movie. Where is the documentation where you found this column name? Commented Mar 1, 2018 at 14:34
  • Seems like the only values for target_type that work are "node" or "target_type" Commented Mar 1, 2018 at 14:54
  • target_id will help to separate out taxonomy and nodes. You can exclude taxonomy terms with this. will $fieldName.entity:content-type:name work for you?
    – Sensimple
    Commented Mar 1, 2018 at 16:35
  • Thanks for the explanation. That format looks like what I need, but it appears to be invalid. The error: The "content_type:name" entity type does not exist. Pretty much anything I put after $fieldName.entity: gives a similar error. According to the docs, entity is the actual entity being referenced, correct? Commented Mar 1, 2018 at 18:36

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.