You are here

public function EntityUsage::listUsage in Entity Usage 8.2

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

Determines where an entity is used (deprecated).

This method should not be used in new integrations, and is only provided as BC-layer for existing implementations. Note however that the count returned on 2.x will be different from the count returned on 1.x, once now we track all revisions / translations independently.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: A target (referenced) entity.

bool $include_method: (optional) Whether the results must be wrapped into an additional array level, by the reference method. Defaults to FALSE.

Return value

array A nested array with usage data.The first level is keyed by the type of the source entity, the second by the referencing objects ID. The value of the second level contains the usage count, which will be summed for all revisions and translations tracked. Note that if $include_method is TRUE, the first level is keyed by the reference method, and the second level will continue as explained above.

Overrides EntityUsageInterface::listUsage

Deprecated

in branch 2.x. Use \Drupal\entity_usage\EntityUsageInterface::listSources() instead.

File

src/EntityUsage.php, line 339

Class

EntityUsage
Defines the entity usage base class.

Namespace

Drupal\entity_usage

Code

public function listUsage(EntityInterface $entity, $include_method = FALSE) {
  $result = $this
    ->listSources($entity);
  $references = [];
  foreach ($result as $source_entity_type => $entity_record) {
    foreach ($entity_record as $entity_id => $records) {
      foreach ($records as $record) {
        if ($include_method) {
          if (empty($references[$record['method']][$source_entity_type][$entity_id])) {

            // This is the first of this entity type/id, just store the count.
            $references[$record['method']][$source_entity_type][$entity_id] = $record['count'];
          }
          else {

            // Sum all counts for different revisions or translations.
            $references[$record['method']][$source_entity_type][$entity_id] += $record['count'];
          }
        }
        else {
          if (empty($references[$source_entity_type][$entity_id])) {

            // This is the first of this entity type/id, just store the count.
            $references[$source_entity_type][$entity_id] = $record['count'];
          }
          else {

            // Sum all counts for different revisions or translations.
            $references[$source_entity_type][$entity_id] += $record['count'];
          }
        }
      }
    }
  }
  return $references;
}