You are here

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

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

Class

EditorDetectorBase

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 $delta => $fieldData) {
          $text = $fieldData->value;
          if ($field_type === 'text_with_summary') {
            $text .= $fieldData->summary;
          }
          $fieldTextEntities = $this
            ->extractEntitiesFromText($text);
          foreach ($fieldTextEntities as $fieldTextEntityTypeUuid => $fieldTextEntityTypeId) {
            $relatedEntity = $this->entityRepository
              ->loadEntityByUuid($fieldTextEntityTypeId, $fieldTextEntityTypeUuid);
            if ($relatedEntity instanceof ContentEntityInterface && $relatedEntity
              ->isTranslatable() && $this->lingotekConfiguration
              ->isEnabled($relatedEntity
              ->getEntityTypeId(), $relatedEntity
              ->bundle())) {
              $entities[$relatedEntity
                ->getEntityTypeId()][$relatedEntity
                ->id()] = $relatedEntity
                ->getUntranslated();
              $entities = $this
                ->extract($relatedEntity, $entities, $related, $depth, $visited);
            }
          }
        }
      }
    }
  }
  return $entities;
}