You are here

public function FieldLinkDetector::extract in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::extract()
  2. 3.6.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::extract()
  3. 3.7.x src/Plugin/RelatedEntitiesDetector/FieldLinkDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\FieldLinkDetector::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/FieldLinkDetector.php, line 92

Class

FieldLinkDetector
@RelatedEntitiesDetector ( id = "field_link_detector", title = Plugin annotation @Translation("Get editor linked entities with html links"), description = @translation("Get editor linked entities with html links."), weight = 7, )

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)) {
        foreach ($entity
          ->get($k) as $item) {
          $target = $this
            ->getTargetEntities($item);
          if (!empty($target)) {
            [
              $target_entity_type_id,
              $target_id,
            ] = $target;
            $target_entity_type = $this->entityTypeManager
              ->getDefinition($target_entity_type_id);
            if ($target_entity_type instanceof ContentEntityType) {
              $referencedEntity = $this->entityTypeManager
                ->getStorage($target_entity_type_id)
                ->load($target_id);
              if ($referencedEntity !== NULL) {

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