You are here

function relation_get_related_entity in Relation 8

Same name and namespace in other branches
  1. 8.2 relation.module \relation_get_related_entity()
  2. 7 relation.module \relation_get_related_entity()

Returns a related entity.

Returns the entity object of the first other entity in the first relation that matches the given conditions. Do not expect to get exactly what you want, especially if you have multiple relations of the same type on the search entity.

Parameters

string $entity_type: The entity type of one of the endpoints.

int $entity_id: The entity id of one of the endpoints.

string|null $relation_type: (optional) The relation type of the relation to find.

int|null $r_index: (optional) The index of the search entity in the relation to be found (0 = source, 1 = target).

Return value

\Drupal\Core\Entity The entity object from the other endpoint.

1 call to relation_get_related_entity()
RelationAPITest::testRelationTypes in src/Tests/RelationAPITest.php
Tests relation types.
1 string reference to 'relation_get_related_entity'
relation_clear_related_entities_cache in ./relation.module
Clear the cache for a set of endpoints.

File

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

Code

function relation_get_related_entity($entity_type, $entity_id, $relation_type = NULL, $r_index = NULL) {

  // Static cache the results of relation_query() and relation_load() to avoid
  // duplicate queries if this is called multiple times with the same arguments
  // during a request.
  $items =& drupal_static(__FUNCTION__);
  $request_key = "{$entity_type}:{$entity_id}";
  $cache_key = "{$request_key}:{$relation_type}:{$r_index}";
  if (isset($items[$cache_key])) {
    $entities = $items[$cache_key];
  }
  elseif ($cached = \Drupal::cache()
    ->get("relation:{$cache_key}")) {
    $entities = $cached->data;
    $items[$cache_key] = $entities;
  }
  else {
    $query = Drupal::entityQuery('relation');
    relation_query_add_related($query, $entity_type, $entity_id, $r_index)
      ->range(0, 1);
    if ($relation_type) {
      $query
        ->condition('relation_type', $relation_type);
    }
    $results = $query
      ->execute();
    $relation_id = reset($results);
    if ($relation_id) {
      $relation = Relation::load($relation_id);
      if ($relation->arity->value == 1) {
        $entities = FALSE;
      }
      else {
        $entities = $relation->endpoints;
      }
    }
    else {
      $entities = FALSE;
    }
    \Drupal::cache()
      ->set("relation:{$cache_key}", $entities);
    $items[$cache_key] = $entities;
  }
  if ($entities) {
    $first_entity_key = $entities[0]->entity_type . ':' . $entities[0]->entity_id;
    if (isset($r_index)) {
      $request_key = $request_key . ':' . $r_index;
      $first_entity_key = $first_entity_key . ':' . $entities[0]->r_index;
    }
    if ($request_key == $first_entity_key) {
      $storage_handler = \Drupal::entityTypeManager()
        ->getStorage($entities[1]->entity_type);
      return $storage_handler
        ->load($entities[1]->entity_id);
    }
    $storage_handler = \Drupal::entityTypeManager()
      ->getStorage($entities[0]->entity_type);
    return $storage_handler
      ->load($entities[0]->entity_id);
  }
  return FALSE;
}