You are here

protected function IdsCleaner::cleanReferenceIds in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Plugin/SyncNormalizerDecorator/IdsCleaner.php \Drupal\content_sync\Plugin\SyncNormalizerDecorator\IdsCleaner::cleanReferenceIds()

Parameters

$normalized_entity:

\Drupal\Core\Entity\ContentEntityInterface $entity:

Return value

mixed

1 call to IdsCleaner::cleanReferenceIds()
IdsCleaner::decorateNormalization in src/Plugin/SyncNormalizerDecorator/IdsCleaner.php

File

src/Plugin/SyncNormalizerDecorator/IdsCleaner.php, line 40

Class

IdsCleaner
Provides a decorator for setting the alias to entity.

Namespace

Drupal\content_sync\Plugin\SyncNormalizerDecorator

Code

protected function cleanReferenceIds(&$normalized_entity, ContentEntityInterface $entity) {
  $field_definitions = $entity
    ->getFieldDefinitions();
  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 (isset($normalized_entity[$field_name]) && !empty($normalized_entity[$field_name]) && is_array($normalized_entity[$field_name])) {
      $entity_type = $field_definition
        ->getFieldStorageDefinition()
        ->getSetting('target_type');
      $reflection = new \ReflectionClass(\Drupal::entityTypeManager()
        ->getDefinition($entity_type)
        ->getClass());
      if (!$reflection
        ->implementsInterface('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
        continue;
      }
      $key = $field_definition
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      foreach ($normalized_entity[$field_name] as &$item) {
        if (!empty($item[$key])) {
          unset($item[$key]);
        }
        if (!empty($item['url'])) {
          unset($item['url']);
        }
      }
    }
  }
  return $normalized_entity;
}