public function LinkItemNormalizer::normalize in Replication 8
Same name and namespace in other branches
- 8.2 src/Normalizer/LinkItemNormalizer.php \Drupal\replication\Normalizer\LinkItemNormalizer::normalize()
Overrides ComplexDataNormalizer::normalize
File
- src/
Normalizer/ LinkItemNormalizer.php, line 53
Class
Namespace
Drupal\replication\NormalizerCode
public function normalize($object, $format = NULL, array $context = []) {
$attributes = [];
foreach ($object
->getProperties(TRUE) as $name => $field) {
$attributes[$name] = $this->serializer
->normalize($field, $format, $context);
}
// For some reasons the options field is not normalized correctly if it
// has more information like attributes added by menu_attributes module.
// The field data will be empty after normalization, so we add missing data
// here.
if (!empty($object
->getValue()['options']) && empty($attributes['options'])) {
$attributes['options'] = $object
->getValue()['options'];
}
// Use the entity UUID instead of ID in urls like internal:/node/1.
if (isset($attributes['uri'])) {
$scheme = parse_url($attributes['uri'], PHP_URL_SCHEME);
if (!in_array($scheme, [
'internal',
'entity',
])) {
return $attributes;
}
$path = parse_url($attributes['uri'], PHP_URL_PATH);
// This service is not injected to avoid circular reference error when
// installing page_manager contrib module.
$url = \Drupal::service('path.validator')
->getUrlIfValidWithoutAccessCheck($path);
if ($url instanceof Url) {
$internal_path = ltrim($url
->getInternalPath(), '/');
$path = ltrim($path, '/');
// Return attributes as they are if uri is an alias.
if ($path != $internal_path) {
return $attributes;
}
$route_name = $url
->getRouteName();
$route_name_parts = explode('.', $route_name);
if ($route_name_parts[0] === 'entity' && $this
->isMultiversionableEntityType($route_name_parts[1])) {
$entity_type = $route_name_parts[1];
$entity_id = $url
->getRouteParameters()[$entity_type];
}
else {
return $attributes;
}
}
else {
return $attributes;
}
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($entity_id);
if ($entity instanceof EntityInterface) {
$entity_uuid = $entity
->uuid();
$attributes['uri'] = str_replace($entity_id, $entity_uuid, $attributes['uri']);
$attributes['_entity_uuid'] = $entity_uuid;
$attributes['_entity_type'] = $entity_type;
$bundle_key = $entity
->getEntityType()
->getKey('bundle');
$bundle = $entity
->bundle();
if ($bundle_key && $bundle) {
$attributes[$bundle_key] = $bundle;
}
}
}
return $attributes;
}