You are here

function relation_query_add_related in Relation 8

Same name and namespace in other branches
  1. 8.2 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 $r_index: (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, $r_index = NULL) {
  $group = $query
    ->andConditionGroup()
    ->condition('endpoints.entity_type', $entity_type, '=');
  if (isset($entity_id)) {
    $operator = is_array($entity_id) ? 'IN' : '=';
    $group
      ->condition('endpoints.entity_id', $entity_id, $operator);
  }
  if (isset($r_index)) {
    $group
      ->condition('endpoints.r_index', $r_index, '=');
  }
  $query
    ->condition($group);
  return $query;
}