Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
namespace Drupal\bullseye_api\Normalizer;
use Drupal\serialization\Normalizer\NormalizerBase;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\node\Entity\Node;
* Custom Normalization for Benefit entities.
class BenefitNormalizer extends ContentEntityNormalizer {
protected $supportedInterfaceOrClass = 'Drupal\Core\TypedData\TypedDataInterface';
public function supportsNormalization($data, $format = NULL) {
// If we aren't dealing with an object or the format is not supported return now.
if (!is_object($data) || !$this->checkFormat($format)) {
return FALSE;
// This custom normalizer should be supported for Article bundle for the Node Entity.
if ($data instanceof Node) {
return ($data->bundle() == 'article');
// Otherwise, this normalizer does not support the $data object.
return FALSE;
* {@inheritdoc}
public function normalize($entity, $format = NULL, array $context = array()) {
$data = [];
$data = [
'id' => $entity->id(),
'type' => $entity->bundle(),
'title' => $entity->title->value,
'summary' => $entity->body->summary,
return $data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.