class MenuLinkContentNormalizer in Replication 8
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\replication\Normalizer\ContentEntityNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface uses FieldableEntityNormalizerTrait
- class \Drupal\replication\Normalizer\MenuLinkContentNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\replication\Normalizer\ContentEntityNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface uses FieldableEntityNormalizerTrait
Expanded class hierarchy of MenuLinkContentNormalizer
1 string reference to 'MenuLinkContentNormalizer'
1 service uses MenuLinkContentNormalizer
File
- src/
Normalizer/ MenuLinkContentNormalizer.php, line 8
Namespace
Drupal\replication\NormalizerView source
class MenuLinkContentNormalizer extends ContentEntityNormalizer implements DenormalizerInterface {
/**
* @var string[]
*/
protected $supportedInterfaceOrClass = [
'Drupal\\menu_link_content\\MenuLinkContentInterface',
];
/**
* @var string[]
*/
protected $format = [
'json',
];
/**
* {@inheritdoc}
*/
public function normalize($data, $format = NULL, array $context = []) {
$normalized = parent::normalize($data, $format, $context);
return $normalized;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
$denormalized = parent::denormalize($data, $class, $format, $context);
$entity_type_id = 'menu_link_content';
$parent = $denormalized instanceof MenuLinkContentInterface ? $denormalized
->getParentId() : NULL;
if (!empty($parent) && strpos($parent, $entity_type_id) === 0) {
list($type, $uuid, ) = explode(':', $parent);
if ($type === 'menu_link_content' && $uuid) {
$storage = $this->entityManager
->getStorage($entity_type_id);
if (!empty($context['workspace'])) {
$storage
->useWorkspace($context['workspace']
->id());
}
$parent = $storage
->loadByProperties([
'uuid' => $uuid,
]);
$parent = reset($parent);
if ($parent instanceof MenuLinkContentInterface && $parent
->id()) {
$denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
->id();
}
elseif (!$parent) {
// Create a new menu link as stub.
$selection_instance = $this->selectionManager
->getInstance([
'target_type' => $entity_type_id,
]);
$parent = $selection_instance
->createNewEntity($entity_type_id, $entity_type_id, rand(), 1);
// Indicate that this revision is a stub.
$parent->_rev->is_stub = TRUE;
$parent->uuid->value = $uuid;
$parent->langcode->value = $data['@context']['@language'];
$parent->link->uri = 'internal:/';
if (!empty($context['workspace'])) {
$parent->workspace->entity = $context['workspace'];
}
$parent->menu_name->value = $denormalized
->getMenuName();
$parent
->save();
$denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
->id();
}
$storage
->useWorkspace(NULL);
}
}
return $denormalized;
}
public function supportsDenormalization($data, $type, $format = NULL) {
if (in_array($type, [
'Drupal\\menu_link_content\\MenuLinkContentInterface',
'Drupal\\Core\\Entity\\ContentEntityInterface',
], true)) {
if (isset($data['@type']) && $data['@type'] == 'menu_link_content') {
return TRUE;
}
}
return FALSE;
}
}