class UserEntityNormalizer in Content Synchronization 8.2
Same name and namespace in other branches
- 3.0.x src/Normalizer/UserEntityNormalizer.php \Drupal\content_sync\Normalizer\UserEntityNormalizer
 
User entity normalizer class.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\serialization\Normalizer\ComplexDataNormalizer
- class \Drupal\serialization\Normalizer\EntityNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface uses FieldableEntityNormalizerTrait
- class \Drupal\serialization\Normalizer\ContentEntityNormalizer
- class \Drupal\content_sync\Normalizer\ContentEntityNormalizer uses SyncNormalizerDecoratorTrait
- class \Drupal\content_sync\Normalizer\UserEntityNormalizer
 
 
 - class \Drupal\content_sync\Normalizer\ContentEntityNormalizer uses SyncNormalizerDecoratorTrait
 
 - class \Drupal\serialization\Normalizer\ContentEntityNormalizer
 
 - class \Drupal\serialization\Normalizer\EntityNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface uses FieldableEntityNormalizerTrait
 
 - class \Drupal\serialization\Normalizer\ComplexDataNormalizer
 
Expanded class hierarchy of UserEntityNormalizer
1 string reference to 'UserEntityNormalizer'
1 service uses UserEntityNormalizer
File
- src/
Normalizer/ UserEntityNormalizer.php, line 8  
Namespace
Drupal\content_sync\NormalizerView source
class UserEntityNormalizer extends ContentEntityNormalizer {
  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = 'Drupal\\user\\UserInterface';
  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $entity = parent::denormalize($data, $class, $format, $context);
    // For security reasons the user 1 is not updated.
    if ((int) $entity
      ->id() === 1) {
      return $entity
        ->load(1);
    }
    // User 0 is not updated.
    if (!empty($data['_content_sync']['is_anonymous']) && (int) $entity
      ->id() === 0) {
      return $entity
        ->load(0);
    }
    return $entity;
  }
  /**
   * {@inheritdoc}
   */
  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;
  }
  /**
   * {@inheritdoc}
   */
  protected function getContentSyncMetadata($object, $context = []) {
    /** @var \Drupal\user\Entity\User $object */
    $metadata = parent::getContentSyncMetadata($object, $context);
    if ($object
      ->isAnonymous()) {
      $metadata['is_anonymous'] = TRUE;
    }
    return $metadata;
  }
}