You are here

public function UriNormalizer::normalize in Tome 8

Overrides PrimitiveDataNormalizer::normalize

File

modules/tome_sync/src/Normalizer/UriNormalizer.php, line 53

Class

UriNormalizer
Normalizer for Uri data.

Namespace

Drupal\tome_sync\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {
  $value = parent::normalize($object, $format, $context);
  if (!empty($value) && strpos($value, 'entity:') === 0) {
    $parts = explode('/', str_replace('entity:', '', $value));
    if (count($parts) >= 2 && $this->entityTypeManager
      ->hasDefinition($parts[0]) && is_numeric($parts[1])) {
      if ($entity = $this->entityTypeManager
        ->getStorage($parts[0])
        ->load($parts[1])) {
        $parts[1] = $entity
          ->uuid();
        $value = 'entity:' . implode('/', $parts);
      }
    }
  }
  return $value;
}