- Issue created by @jsutta
- π¨π΄Colombia camilo.escobar
I created a field enhancer and was also hoping to provide sort of default value when the field han an empty or NULL value, but I found the same: field enhancers doesn't run when the field is empty. It would be useful to make it work to transform empty values as well.
- π¨πΏCzech Republic siva01
We have resolved this issue by using a custom Drupal\custom_module\Normalizer, where the function normalizeFieldItems has been rewritten to provide a default value if the field value is NULL.
It works, but it requires an extension of the @internal class FieldNormalizer. So I am not sure if it is the best practice, but it works.
- π¨π΄Colombia camilo.escobar
Hi @siva01! Thanks for sharing your solution.
I assume you decorated the service
serializer.normalizer.field.jsonapi
via an impostor normalizer and overwrote the functionnormalizeFieldItems()
. It's a clever solution, interesting approach.For other people who want to go down this path, it is important to note that there is another party involved in the implementation of an impostor normalizer (in addition to the links you provided above): a Service Provider class. That's necessary to enable the
/src-impostor-normalizers
folder to host impostor classes within the\Drupal\jsonapi\Normalizer
namespace.