You are here

protected function ContentEntityNormalizer::fixReferences in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Normalizer/ContentEntityNormalizer.php \Drupal\content_sync\Normalizer\ContentEntityNormalizer::fixReferences()

Parameters

array $data:

$entity_type_id:

Return value

array

1 call to ContentEntityNormalizer::fixReferences()
ContentEntityNormalizer::denormalize in src/Normalizer/ContentEntityNormalizer.php
Denormalizes data back into an object of the given class.

File

src/Normalizer/ContentEntityNormalizer.php, line 246

Class

ContentEntityNormalizer
Adds the file URI to embedded file entities.

Namespace

Drupal\content_sync\Normalizer

Code

protected function fixReferences(&$data, $entity_type_id, $bundle = FALSE) {
  if ($bundle) {
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($entity_type_id, $bundle);
  }
  else {
    $bundles = array_keys($this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id));
    $field_definitions = [];
    foreach ($bundles as $bundle) {
      $field_definitions_bundle = $this->entityFieldManager
        ->getFieldDefinitions($entity_type_id, $bundle);
      if (is_array($field_definitions_bundle)) {
        $field_definitions += $field_definitions_bundle;
      }
    }
  }
  foreach ($field_definitions as $field_name => $field_definition) {

    // We are only interested in importing content entities.
    if (!is_a($field_definition
      ->getClass(), '\\Drupal\\Core\\Field\\EntityReferenceFieldItemList', TRUE)) {
      continue;
    }
    if (!empty($data[$field_name]) && is_array($data[$field_name])) {
      $key = $field_definition
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      foreach ($data[$field_name] as $i => &$item) {
        if (!empty($item['target_uuid'])) {
          $reference = $this->entityRepository
            ->loadEntityByUuid($item['target_type'], $item['target_uuid']);
          if ($reference) {
            $item[$key] = $reference
              ->id();
            if (is_a($reference, RevisionableInterface::class, TRUE)) {
              $item['target_revision_id'] = $reference
                ->getRevisionId();
            }
          }
          else {
            $reflection = new \ReflectionClass($this->entityTypeManager
              ->getStorage($item['target_type'])
              ->getEntityType()
              ->getClass());
            if ($reflection
              ->implementsInterface(ContentEntityInterface::class)) {
              unset($data[$field_name][$i]);
            }
          }
        }
      }
    }
  }
  return $data;
}