You are here

public function ContentEntityNormalizer::normalize in Content Synchronization 3.0.x

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

Overrides ContentEntityNormalizer::normalize

2 calls to ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in src/Normalizer/FileEntityNormalizer.php
UserEntityNormalizer::normalize in src/Normalizer/UserEntityNormalizer.php
2 methods override ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in src/Normalizer/FileEntityNormalizer.php
UserEntityNormalizer::normalize in src/Normalizer/UserEntityNormalizer.php

File

src/Normalizer/ContentEntityNormalizer.php, line 119

Class

ContentEntityNormalizer
Adds the file URI to embedded file entities.

Namespace

Drupal\content_sync\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {

  /* @var ContentEntityInterface $object */
  $normalized_data = parent::normalize($object, $format, $context);
  $normalized_data['_content_sync'] = $this
    ->getContentSyncMetadata($object, $context);

  /**
   * @var \Drupal\Core\Entity\ContentEntityBase $object
   */
  $referenced_entities = $object
    ->referencedEntities();

  // Add node uuid for menu link if any.
  if ($object
    ->getEntityTypeId() == 'menu_link_content') {
    if ($entity = $this
      ->getMenuLinkNodeAttached($object)) {
      $normalized_data['_content_sync']['menu_entity_link'][$entity
        ->getEntityTypeId()] = $entity
        ->uuid();
      $referenced_entities[] = $entity;
    }
  }

  // Add node uuid for path alias if any.
  if ($object
    ->getEntityTypeId() == 'path_alias') {
    if ($entity = $this
      ->getPathLinkNodeAttached($object)) {
      $normalized_data['_content_sync']['path_alias'][$entity
        ->getEntityTypeId()] = $entity
        ->uuid();
      $referenced_entities[] = $entity;
    }
  }
  if (!empty($referenced_entities)) {
    $dependencies = [];
    foreach ($referenced_entities as $entity) {
      $reflection = new \ReflectionClass($entity);
      if ($reflection
        ->implementsInterface(ContentEntityInterface::class)) {
        $ids = [
          $entity
            ->getEntityTypeId(),
          $entity
            ->bundle(),
          $entity
            ->uuid(),
        ];
        $dependency = implode(ContentSyncManager::DELIMITER, $ids);
        if (!$this
          ->inDependencies($dependency, $dependencies)) {
          $dependencies[$entity
            ->getEntityTypeId()][] = $dependency;
        }
      }
    }
    $normalized_data['_content_sync']['entity_dependencies'] = $dependencies;
  }

  // Decorate normalized entity before retuning it.
  if (is_a($object, ContentEntityInterface::class, TRUE)) {
    $this
      ->decorateNormalization($normalized_data, $object, $format, $context);
  }
  return $normalized_data;
}