public function MenuLinkContentNormalizer::normalize in Better Normalizers 8
Overrides ContentEntityNormalizer::normalize
File
- src/
Normalizer/ MenuLinkContentNormalizer.php, line 67
Class
- MenuLinkContentNormalizer
- A normalizer to handle menu-link content links to entities.
Namespace
Drupal\better_normalizers\NormalizerCode
public function normalize($entity, $format = NULL, array $context = array()) {
$normalized = parent::normalize($entity, $format, $context);
if (isset($normalized['link']) && is_array($normalized['link'])) {
foreach ($normalized['link'] as $key => $link) {
try {
$stub = EntityStub::fromEntityUri($link['uri']);
try {
if ($target_entity = $this->entityTypeManager
->getStorage($stub
->getEntityTypeId())
->load($stub
->getEntityId())) {
$normalized = $this
->embedEntity($entity, $format, $context, $target_entity, $normalized, self::PSUEDO_FIELD_NAME);
$normalized['link'][$key] += [
'target_uuid' => $target_entity
->uuid(),
];
}
else {
// Entity ID no longer exists.
continue;
}
} catch (PluginNotFoundException $e) {
// Entity-type not found.
continue;
}
} catch (\InvalidArgumentException $e) {
// Not an Entity URI link.
continue;
}
}
}
if (isset($normalized['parent']) && is_array($normalized['parent'])) {
foreach ($normalized['parent'] as $parent) {
if (strpos($parent['value'], PluginBase::DERIVATIVE_SEPARATOR) !== FALSE) {
list($plugin_id, $parent_uuid) = explode(PluginBase::DERIVATIVE_SEPARATOR, $parent['value']);
if ($plugin_id === 'menu_link_content' && ($parent_entity = $this->entityRepository
->loadEntityByUuid('menu_link_content', $parent_uuid))) {
// This entity has a parent menu link entity, we embed it.
$normalized = $this
->embedEntity($entity, $format, $context, $parent_entity, $normalized, self::PSUEDO_PARENT_FIELD_NAME);
}
}
}
}
return $normalized;
}