You are here

public function EntityUsage::listTargets in Entity Usage 8.2

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

Provide a list of all referenced target entities for a source entity.

Parameters

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

int $vid: The revision id to return the references for. Defaults to all revisions.

Return value

array A nested array with usage data. The first level is keyed by the type of the target entities, the second by the target id. The value of the second level contains all other information like the method used by the source to reference the target, the field name and the target language code.

Overrides EntityUsageInterface::listTargets

See also

\Drupal\entity_usage\EntityUsageInterface::listSources()

1 call to EntityUsage::listTargets()
EntityUsage::listReferencedEntities in src/EntityUsage.php
Determines referenced entities (deprecated).

File

src/EntityUsage.php, line 279

Class

EntityUsage
Defines the entity usage base class.

Namespace

Drupal\entity_usage

Code

public function listTargets(EntityInterface $source_entity, $vid = NULL) {

  // Entities can have string IDs. We support that by using different columns
  // on each case.
  $source_id_column = $this
    ->isInt($source_entity
    ->id()) ? 'source_id' : 'source_id_string';
  $query = $this->connection
    ->select($this->tableName, 'e')
    ->fields('e', [
    'target_id',
    'target_id_string',
    'target_type',
    'method',
    'field_name',
    'count',
  ])
    ->condition($source_id_column, $source_entity
    ->id())
    ->condition('source_type', $source_entity
    ->getEntityTypeId())
    ->condition('count', 0, '>')
    ->orderBy('target_id', 'DESC');
  if ($vid) {
    $query
      ->condition('source_vid', $vid);
  }
  $result = $query
    ->execute();
  $references = [];
  foreach ($result as $usage) {
    $target_id_value = !empty($usage->target_id) ? $usage->target_id : $usage->target_id_string;
    $references[$usage->target_type][(string) $target_id_value][] = [
      'method' => $usage->method,
      'field_name' => $usage->field_name,
      'count' => $usage->count,
    ];
  }
  return $references;
}