Skip to content

Instantly share code, notes, and snippets.

@kevinquillen
Created April 23, 2018 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kevinquillen/6bf63236ff3dc47e6007048adee821d3 to your computer and use it in GitHub Desktop.
Provides an entity normalizer for certain node types.
<?php
declare(strict_types = 1);
namespace Drupal\harlib_restful_normalizer\Normalizer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\harlib_libcal_api\LibCalApi;
use Drupal\image\Entity\ImageStyle;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;
use Drupal\node\NodeInterface;
/**
* Converts the Drupal entity object structures to a normalized array.
*/
class NodeEntityNormalizer extends ContentEntityNormalizer {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = [
'Drupal\node\NodeInterface',
];
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
$type = $entity->getType();
$methods = [
'library' => 'normalizeLibrary',
'collection' => 'normalizeCollection',
];
if (isset($methods[$type])) {
return $this->{$methods[$type]}($entity, $format, $context);
}
return parent::normalize($entity, $format, $context);
}
/**
* Normalize a library content type.
*/
protected function normalizeLibrary($entity) {
$attributes['id'] = $entity->id();
$attributes['featured'] = !empty($entity->get('field_is_coming_soon')->value);
$attributes['external'] = !empty($entity->get('field_is_stub')->value) && !empty($entity->get('field_website_link')[0]);
$attributes['name'] = $entity->label();
$attributes['subTitle'] = !empty($entity->get('field_subtitle')->value) ? $entity->get('field_subtitle')->value : '';
return $attributes;
}
/**
* Normalize a collection content type.
*
* @param \Drupal\node\NodeInterface $entity
* The node entity.
*
* @return array
* The item attributes.
*/
protected function normalizeCollection(NodeInterface $entity) : array {
$attributes = [];
$attributes['id'] = $entity->id();
$attributes['title'] = $entity->label();
$attributes['subtitle'] = $entity->get('field_subtitle')->value;
$attributes['summary'] = $entity->get('field_summary')->value;
$attributes['card_type'] = $entity->get('field_directory_view')->value;
$attributes['list_image'] = NULL;
$attributes['card_image'] = NULL;
$attributes['url'] = Url::fromRoute('entity.node.canonical', ['node' => $attributes['id']])->toString();
$media = $entity->get('field_featured_image')->getValue();
if (!empty($media)) {
$item = $this->entityTypeManager->getStorage('media')->load($media[0]['target_id']);
$image = $item->get('image')->getValue();
$attributes['list_image'] = [
'src' => ImageStyle::load('collection_listing')
->buildUrl($item->image->entity->getFileUri()),
'alt' => $image[0]['alt'],
];
switch ($attributes['card_type']) {
case 'wide':
$attributes['card_image'] = [
'src' => ImageStyle::load('collection_listing_wide')
->buildUrl($item->image->entity->getFileUri()),
'alt' => $image[0]['alt'],
];
break;
case 'tall':
$attributes['card_image'] = [
'src' => ImageStyle::load('collection_listing_tall')
->buildUrl($item->image->entity->getFileUri()),
'alt' => $image[0]['alt'],
];
break;
case 'standard':
$attributes['card_image'] = $attributes['list_image'];
break;
default:
break;
}
}
return $attributes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment