You are here

private function Exporter::getEntityReferencesRecursive in Default Content Deploy 8

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::getSerializedContentWithReferences in src/Exporter.php
Exports a single entity and all its referenced entity.

File

src/Exporter.php, line 585

Class

Exporter
A service for handling export of default content.

Namespace

Drupal\default_content_deploy

Code

private 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) {
      $indexed_dependencies += $this
        ->getEntityReferencesRecursive($dependent_entity, $depth + 1, $indexed_dependencies);
    }
  }
  return $indexed_dependencies;
}