You are here

public function EntityReferenceDetectorBase::extract in Lingotek Translation 3.7.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/EntityReferenceDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EntityReferenceDetectorBase::extract()
  2. 3.6.x src/Plugin/RelatedEntitiesDetector/EntityReferenceDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EntityReferenceDetectorBase::extract()
  3. 3.8.x src/Plugin/RelatedEntitiesDetector/EntityReferenceDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EntityReferenceDetectorBase::extract()

Extract nested and related content.

Parameters

Drupal\Core\Entity\ContentEntityInterface $entity: Entity node to parse for related entities.

array $entities: Entities found.

array $related: Related entities.

int $depth: Recursion depth of entity node.

array $visited: Array of visited field definitions.

Return value

array An array of the nested content

Overrides RelatedEntitiesDetectorInterface::extract

File

src/Plugin/RelatedEntitiesDetector/EntityReferenceDetectorBase.php, line 78

Class

EntityReferenceDetectorBase

Namespace

Drupal\lingotek\Plugin\RelatedEntitiesDetector

Code

public function extract(ContentEntityInterface &$entity, array &$entities, array &$related, $depth, array $visited) {
  $visited[$entity
    ->bundle()][] = $entity
    ->id();
  $entities[$entity
    ->getEntityTypeId()][$entity
    ->id()] = $entity
    ->getUntranslated();
  if ($depth > 0) {
    --$depth;
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    foreach ($field_definitions as $k => $definition) {
      $field_type = $field_definitions[$k]
        ->getType();
      if (in_array($field_type, $this->fieldTypes)) {
        $target_entity_type_id = $field_definitions[$k]
          ->getFieldStorageDefinition()
          ->getSetting('target_type');
        $target_entity_type = $this->entityTypeManager
          ->getDefinition($target_entity_type_id);
        if ($target_entity_type instanceof ContentEntityType) {
          $child_entities = $entity
            ->get($k)
            ->referencedEntities();
          foreach ($child_entities as $embedded_entity) {
            if ($embedded_entity !== NULL) {

              // We need to avoid cycles if we have several entity references
              // referencing each other.
              if (!isset($visited[$embedded_entity
                ->bundle()]) || !in_array($embedded_entity
                ->id(), $visited[$embedded_entity
                ->bundle()])) {
                if ($embedded_entity instanceof ContentEntityInterface && $embedded_entity
                  ->isTranslatable() && $this->lingotekConfiguration
                  ->isEnabled($embedded_entity
                  ->getEntityTypeId(), $embedded_entity
                  ->bundle())) {
                  if (!$this->lingotekConfiguration
                    ->isFieldLingotekEnabled($entity
                    ->getEntityTypeId(), $entity
                    ->bundle(), $k)) {
                    $entities = $this
                      ->extract($embedded_entity, $entities, $related, $depth, $visited);
                  }
                  else {
                    $related[$embedded_entity
                      ->getEntityTypeId()][$embedded_entity
                      ->id()] = $embedded_entity
                      ->getUntranslated();
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return $entities;
}