Entity Query custom fields#
Generally speaking, querying for custom field properties is no different from querying for any other type of field properties, but there are scenarios where some differences in how you query are required. One such case is when you have a custom field entity reference subfield type, and you want to query it for fields on the actual entity being referenced.
Let's assume you have a custom field type named field_custom and an Entity
reference subfield named reference which
can reference nodes. The nodes have a field called field_number that you want
to filter by in your query. With a normal entity reference field in an entity
query, we could do something like this to get the nested field value of the
referenced node:
1 2 3 | |
This works because the field_reference entity reference field has a main
property on the target_id and the computed entity property along with the
handler settings defined in the field definition can resolve to an entity and
therefore drill down to the field_number value.
In custom_field types, there is not a main property entityQuery can work with, so we cannot derive the values in the same way.
The custom_field way of doing this for the same scenario would look like this:
1 2 3 | |
This works for the following reasons:
field_customis used as the base table.- Although the main property is null, the next specifier
referenceis a real column that takes precedence and the key is advanced. - Every
entity_referencesubfield has a computed property for the entity with the syntax of{subfield_name}__entitywhich in this case results inreference__entity. reference__entity:nodeis used as the relationship_specifier. Since it's in the property definitions and is type DataReferenceDefinitionInterface, it joins the node table.
Note
Due to the fact that there is no main property in custom field types, the
following query would also fail as the ->exists defaults to the main
property which of course is purposely set to NULL on our field types:
1 2 3 | |