Last active
May 18, 2018 22:00
-
-
Save cmcintosh/78c10494aa0f7bc13f5dc6b5b3f7b9da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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