You are here

protected function EntityUsageTrackBase::getAllBottomLevelTargets in Entity Usage 8.3

Calculates all bottom-level targets for a given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The source entity.

array $targets: (optional) For internal use only.

Return value

array An indexed array of bottom-level target entities that are referenced by the passed-in entity, or by any middle-level entity that is referenced by it.

2 calls to EntityUsageTrackBase::getAllBottomLevelTargets()
EntityUsageTrackBase::trackOnEntityCreation in src/EntityUsageTrackBase.php
Track usage updates on the creation of entities.
EntityUsageTrackBase::trackOnEntityUpdate in src/EntityUsageTrackBase.php
Track usage updates on the edition of entities.

File

src/EntityUsageTrackBase.php, line 229

Class

EntityUsageTrackBase
Base implementation for track plugins.

Namespace

Drupal\entity_usage

Code

protected function getAllBottomLevelTargets(EntityInterface $entity, array $targets = []) {
  if (!$entity instanceof FieldableEntityInterface) {
    return $targets;
  }
  $trackable_field_types = $this
    ->getApplicableFieldTypes();
  $fields = array_keys($this
    ->getReferencingFields($entity, $trackable_field_types));
  foreach ($fields as $field_name) {
    if ($entity
      ->hasField($field_name) && !$entity->{$field_name}
      ->isEmpty()) {

      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      foreach ($entity->{$field_name} as $field_item) {
        $target_entities = $this
          ->getTargetEntities($field_item);
        foreach ($target_entities as $target_type_and_id) {
          if (substr_count($target_type_and_id, '|') === 2) {
            list($target_type, , $target_revision_id) = explode("|", $target_type_and_id);
            $target_entity = $this->entityTypeManager
              ->getStorage($target_type)
              ->loadRevision($target_revision_id);
          }
          else {
            list($target_type, $target_id) = explode("|", $target_type_and_id);
            $target_entity = $this->entityTypeManager
              ->getStorage($target_type)
              ->load($target_id);
          }
          if (EntityUsageSourceLevel::isBottomLevel($target_entity)) {
            $targets[] = $target_type_and_id;
          }
          else {
            $targets = array_merge($targets, $this
              ->getAllBottomLevelTargets($target_entity, $targets));
          }
        }
      }
    }
  }
  return array_unique($targets);
}