Score:1

How can I extend Serializer to handle a custom field type?

cn flag

I'm trying to extend Serialization to get the JSON Field module to return a JSON object instead of stringified JSON.

Goal: When I access an entity that contains a JSON field using JSON:API, I want a JSON object for the field value (currently, the stringified JSON is returned).

I think what I need to do is to extend one of the core normalizers and use it to add a json_decode() step.

There is some documentation on how Serializer handles entities but it is out-of-date (last updated in 2017 before JSON:API was part of core, and the current code looks nothing like what is shown.)

I tried extending FieldNormalizer in /json_field/src/Normalizer/JsonFieldNormalizer.php.
However, I'm not confident that this is the correct approach, as there is also a FieldItemNormalizer and TypedDataNormalizer. To change how a field is output in JSON:API, which class should I extend?

<?php

namespace Drupal\json_field\Normalizer;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\json_field\Plugin\Field\FieldType\NativeJSONItem;
use Drupal\jsonapi\JsonApiResource\ResourceObject;
use Drupal\jsonapi\Normalizer\FieldNormalizer;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi\ResourceType\ResourceType;

class JsonFieldNormalizer extends FieldNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = NativeJSONItem::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($field, $format = NULL, array $context = []) {
    $field_name = $field->getName();
    \Drupal::logger('json_field')->info("Field has name $field_name");
    assert($field instanceof NativeJSONItem);
    /** @var \Drupal\Core\Field\FieldItemListInterface $field */ $normalized_items = $this->normalizeFieldItems($field, $format, $context);
    assert($context['resource_object'] instanceof ResourceObject);
    return $context['resource_object']->getResourceType()->getFieldByInternalName($field->getName())->hasOne()
      ? array_shift($normalized_items) ?: CacheableNormalization::permanent(NULL)
      : CacheableNormalization::aggregate($normalized_items);
  }

And I added the class to json_field.services.yml:

services:
  json_field.views:
    class: Drupal\json_field\JSONViews
  json_field.normalizer.field.json_field:
    class: Drupal\json_field\Normalizer\JsonFieldNormalizer
    tags:
      - { name: normalizer, priority: 1 }

However, after rebuilding the cache and accessing some JSON fields via JSON:API, I don't see anything in the logs, so it seems like the class is not being picked up.

4uk4 avatar
cn flag
There are a few things which are inconsistent. You are extending FieldNormalizer but $supportedInterfaceOrClass is that of a field item. So better extend FieldItemNormalizer. You are implementing a jsonapi serializer but the service tag is not `{ name: jsonapi_normalizer, priority: 1 }`.
cn flag
@4uk4 Thanks, I originally tried adding it as a `jsonapi_normalizer`, but when I did that, I got the error `LogicException: JSON:API does not allow adding more normalizers!` I just tried extending FieldItemNormalizer and I'm getting the same problem (no logging is triggered when I access the field through JSON:API).
4uk4 avatar
cn flag
OK, I've just tried pointing out the inconsistencies. When they say @internal they really mean it. Reading the comments in [this issue](https://www.drupal.org/project/drupal/issues/2923779) you probably need to add to your custom field type a custom typed data as well (you can simply extend the DataDefinition you now have defined in the field properties) and then define a normalizer for this.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.