You are here

protected function Exporter::getEntityReferencesRecursive in Default Content for D8 8

Same name and namespace in other branches
  1. 2.0.x src/Exporter.php \Drupal\default_content\Exporter::getEntityReferencesRecursive()

Returns all referenced entities of an entity.

This method is also recursive to support use-cases like a node -> media -> file.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

int $depth: Guard against infinite recursion.

\Drupal\Core\Entity\ContentEntityInterface[] $indexed_dependencies: Previously discovered dependencies.

Return value

\Drupal\Core\Entity\ContentEntityInterface[] Keyed array of entities indexed by entity type and ID.

1 call to Exporter::getEntityReferencesRecursive()
Exporter::exportContentWithReferences in src/Exporter.php
Exports a single entity and all its referenced entity.

File

src/Exporter.php, line 278

Class

Exporter
A service for handling import of default content.

Namespace

Drupal\default_content

Code

protected function getEntityReferencesRecursive(ContentEntityInterface $entity, $depth = 0, array &$indexed_dependencies = []) {
  $entity_dependencies = $entity
    ->referencedEntities();
  foreach ($entity_dependencies as $dependent_entity) {

    // Config entities should not be exported but rather provided by default
    // config.
    if (!$dependent_entity instanceof ContentEntityInterface) {
      continue;
    }

    // Using UUID to keep dependencies unique to prevent recursion.
    $key = $dependent_entity
      ->uuid();
    if (isset($indexed_dependencies[$key])) {

      // Do not add already indexed dependencies.
      continue;
    }
    $indexed_dependencies[$key] = $dependent_entity;

    // Build in some support against infinite recursion.
    if ($depth < 6) {

      // @todo Make $depth configurable.
      $indexed_dependencies += $this
        ->getEntityReferencesRecursive($dependent_entity, $depth + 1, $indexed_dependencies);
    }
  }
  return $indexed_dependencies;
}