UserEntityNormalizer.php in Content Synchronization 8.2
File
src/Normalizer/UserEntityNormalizer.php
View source
<?php
namespace Drupal\content_sync\Normalizer;
class UserEntityNormalizer extends ContentEntityNormalizer {
protected $supportedInterfaceOrClass = 'Drupal\\user\\UserInterface';
public function denormalize($data, $class, $format = NULL, array $context = []) {
$entity = parent::denormalize($data, $class, $format, $context);
if ((int) $entity
->id() === 1) {
return $entity
->load(1);
}
if (!empty($data['_content_sync']['is_anonymous']) && (int) $entity
->id() === 0) {
return $entity
->load(0);
}
return $entity;
}
public function normalize($object, $format = NULL, array $context = []) {
$normalized_data = parent::normalize($object, $format, $context);
if (!empty($context['content_sync'])) {
$normalized_data['pass'] = [
[
'value' => $object
->getPassword(),
'pre_hashed' => TRUE,
],
];
$normalized_data['mail'] = [
'value' => $object
->getEmail(),
];
$normalized_data['status'] = [
'value' => $object
->get('status')->value,
];
$normalized_data['roles'] = $object
->getRoles();
}
return $normalized_data;
}
protected function getContentSyncMetadata($object, $context = []) {
$metadata = parent::getContentSyncMetadata($object, $context);
if ($object
->isAnonymous()) {
$metadata['is_anonymous'] = TRUE;
}
return $metadata;
}
}