Skip to content

Instantly share code, notes, and snippets.

@cmcintosh
Last active May 18, 2018 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcintosh/78c10494aa0f7bc13f5dc6b5b3f7b9da to your computer and use it in GitHub Desktop.
Save cmcintosh/78c10494aa0f7bc13f5dc6b5b3f7b9da to your computer and use it in GitHub Desktop.
<?php
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