You are here

function relation_query_add_related in Relation 8.2

Same name and namespace in other branches
  1. 8 relation.module \relation_query_add_related()

Add a related entity to the query.

@todo rename. / extend class ala RelationQuery::related()

Parameters

QueryInterface $query: The query object.

string $entity_type: Entity type of the related entity.

int $entity_id: (optional) Entity id of the related entity. Can be an array of entity IDs.

int|null $delta: (optional) The index of the related entity within the requested relation(s).

Return value

Drupal\Core\Entity\Query\QueryInterface The query object

2 calls to relation_query_add_related()
RelationRepository::relationExists in src/Entity/RelationRepository.php
Checks if a relation exists.
relation_query in ./relation.module
Returns a query object to find related entities.

File

./relation.module, line 163
Describes relations between entities.

Code

function relation_query_add_related(QueryInterface $query, $entity_type, $entity_id = NULL, $delta = NULL) {
  $group = $query
    ->andConditionGroup()
    ->condition('endpoints.%delta.target_type', $entity_type, '=');
  if (isset($entity_id)) {
    $operator = is_array($entity_id) ? 'IN' : '=';
    $group
      ->condition('endpoints.%delta.target_id', $entity_id, $operator);
  }
  if (isset($delta)) {
    $group
      ->condition('endpoints.%delta', $delta, '=');
  }
  $query
    ->condition($group);
  return $query;
}