I'm trying to convert entity objects into array but it's not working. Can you please help me spotting the mistake. I need to use `serializer.normalizer.content_entity' service. The code I have written is:

class ComplexMergeResolver implements ConflictResolverInterface {

  protected $serializer;

  protected function setUp() {
    $this->serializer = $this->container->get('serializer.normalizer.content_entity');

   * {@inheritdoc}
  public function applies() {
   return TRUE;

   * @param RevisionableInterface $revision1
   * @param RevisionableInterface $revision2
   * @param RevisionableInterface $revision3
   * @return array
  public function merge(RevisionableInterface $revision1, RevisionableInterface $revision2, RevisionableInterface $revision3) {
    $r1_array = $this->serializer->normalize($revision1, 'array');
    $r2_array = $this->serializer->normalize($revision2, 'array');
    $r3_array = $this->serializer->normalize($revision3, 'array');
    $merge = new ThreeWayMerge();
    $result = $merge->performMerge($r1_array, $r2_array, $r3_array);
    return $result;

r1_array is returned empty, which means normalize method is not being called properly. Any help would do great. Thanks in advance.


I tried serializer.normalizer.content_entity but got no success in normalizing the (node) revision entity, or even node entity for that matter. It always resulted in the following error:

PHP Fatal error: Call to a member function normalize() on a non-object in core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php on line 28

For this I had to enable the serialization module. I also tried with the ContentEntityNormalizer provided by the "hal" module from core. Result same error:

PHP Fatal error: Call to a member function normalize() on a non-object in core/modules/hal/src/Normalizer/ContentEntityNormalizer.php on line 95

I had success with directly using the serializer service from the serialization module. The working code for serializing a node revision is given below. It was verified on /devel/php

$revision = node_revision_load(4);
$container = \Drupal::getContainer();
$normalizer = $container->get('serializer');
$normalized_array = $serializer->normalize($revision);

Or combine line 2 and 3 above to get the serializer directly as

$serializer = \Drupal::service('serializer');

In your setup() function, you need the following change:

$this->serializer = $this->container->get('serializer');

P.S: I am still trying to figure out why serializer.normalizer.content_entity is not working and throwing the errors.

  • Yes, I think too that the correct service to use is the serializer one. I cannot say why there are two services to serialize objects, but I would think that just one is the one that modules would use in their code. – kiamlaluno Aug 15 '16 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.