Drupal Answers Asked by Kevinvhengst on February 1, 2021
I am trying to figure out how to make a custom normalizer for an entity work. I’ve added the normalizer to my services.yml
with the required tag and arguments and run into no errors. But when retrieving the data, my normalizer is not getting instantiated.
What I am trying to accomplish, is to add a attribute to an OrderItem
entity, this attribute is dynamic and has to be recalculated everytime the order items gets requests through the JSON API.
My call: http://localhost:3000/jsonapi/order-items/default/438f58e9-37b1-412c-be4d-b843caa8d744
So I get get the requested order item, but my normalizer is never run.
OrderItemEntityNormalizer.php
<?php
namespace DrupalxxxNormalizer;
use DrupalserializationNormalizerContentEntityNormalizer;
use Drupalcommerce_orderEntityOrderItemInterface;
/**
* Order Item Normalizer.
*/
class OrderItemEntityNormalizer extends ContentEntityNormalizer {
/**
* Entity interface for this normalizer.
*
* @var string
*/
protected $supportedInterfaceOrClass = OrderItemInterface::class;
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
$attributes = parent::normalize($entity, $format, $context);
$attributes['foo'] = 'xx';
return $attributes;
}
}
services.yml
services:
xxx.order_line_entity:
class: DrupalxxxNormalizerOrderItemEntityNormalizer
arguments: ['@entity_type.manager', '@entity_type.repository', '@entity_field.manager']
tags:
- { name: normalizer, priority: 1 }
I am not getting any warning or error messages. When adding wrong arguments to the service on purpose it does throw errors, so the services file is being read by the collector.
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP