You are here

protected function Exporter::getEntityReferencesRecursive in Default Content for D8 2.0.x

Same name and namespace in other branches
  1. 8 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 199

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;
    }

    // Do not export user 0 or 1.
    if ($dependent_entity instanceof UserInterface && \in_array($dependent_entity
      ->id(), [
      0,
      1,
    ])) {
      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;
    }

    // Do not export composite entity types directly but include their
    // children.
    if (!$dependent_entity
      ->getEntityType()
      ->get('entity_revision_parent_type_field')) {
      $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;
}