You are here

protected function DefaultContentManager::getEntityReferencesRecursive in Default Content 8

Returns all referenced entities of an entity.

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

Parameters

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

int $depth: Guard against infinite recursion.

Return value

\Drupal\Core\Entity\EntityInterface[]

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

File

src/DefaultContentManager.php, line 209
Contains \Drupal\defaultcontent\DefaultContentManager. @todo remove all references to linkmanager?

Class

DefaultContentManager
A service for handling import of default content. @todo throw useful exceptions

Namespace

Drupal\defaultcontent

Code

protected function getEntityReferencesRecursive(ContentEntityInterface $entity, $depth = 0) {
  $entity_dependencies = $entity
    ->referencedEntities();
  foreach ($entity_dependencies as $id => $dependent_entity) {

    // Config entities should not be exported but rather provided by default
    // config.
    if ($dependent_entity instanceof ConfigEntityInterface) {
      unset($entity_dependencies[$id]);
    }
    else {
      $entity_dependencies = array_merge($entity_dependencies, $this
        ->getEntityReferencesRecursive($dependent_entity, $depth + 1));
    }
  }

  // Build in some support against infinite recursion.
  if ($depth > 5) {
    return $entity_dependencies;
  }
  return array_unique($entity_dependencies, SORT_REGULAR);
}