You are here

public function EntityUsage::listReferencedEntities in Entity Usage 8

Same name and namespace in other branches
  1. 8.4 src/EntityUsage.php \Drupal\entity_usage\EntityUsage::listReferencedEntities()
  2. 8.2 src/EntityUsage.php \Drupal\entity_usage\EntityUsage::listReferencedEntities()

Determines referenced entities.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to check for references.

Return value

array A nested array with usage data. The first level is keyed by the type of the referencing entities, the second by the referencing objects id. The value of the second level contains the usage count.

Overrides EntityUsageInterface::listReferencedEntities

File

src/EntityUsage.php, line 178

Class

EntityUsage
Defines the entity usage base class.

Namespace

Drupal\entity_usage

Code

public function listReferencedEntities(EntityInterface $entity) {
  $result = $this->connection
    ->select($this->tableName, 'e')
    ->fields('e', [
    't_id',
    't_type',
    'count',
  ])
    ->condition('re_id', $entity
    ->id())
    ->condition('re_type', $entity
    ->getEntityTypeId())
    ->condition('count', 0, '>')
    ->execute();
  $references = [];
  foreach ($result as $usage) {
    $count = $usage->count;

    // If there were previous usages recorded for this same pair of entities
    // (with different methods), sum on the top of it.
    if (!empty($references[$usage->t_type][$usage->t_id])) {
      $count += $references[$usage->t_type][$usage->t_id];
    }
    $references[$usage->t_type][$usage->t_id] = $count;
  }
  return $references;
}