View source
<?php
namespace Drupal\content_sync\Normalizer;
use Drupal\content_sync\ContentSyncManager;
use Drupal\content_sync\Plugin\SyncNormalizerDecoratorManager;
use Drupal\content_sync\Plugin\SyncNormalizerDecoratorTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\serialization\Normalizer\ContentEntityNormalizer as BaseContentEntityNormalizer;
class ContentEntityNormalizer extends BaseContentEntityNormalizer {
use SyncNormalizerDecoratorTrait;
protected $decoratorManager;
protected $entityTypeBundleInfo;
protected $entityRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, SyncNormalizerDecoratorManager $decorator_manager) {
parent::__construct($entity_type_manager, $entity_type_repository, $entity_field_manager);
$this->decoratorManager = $decorator_manager;
$this->entityRepository = $entity_repository;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public function denormalize($data, $class, $format = NULL, array $context = []) {
if (is_null($data)) {
return NULL;
}
$original_data = $data;
$entity_type_id = !empty($context['entity_type']) ? $context['entity_type'] : $this->entityTypeRepository
->getEntityTypeFromClass($class);
$bundle = FALSE;
$entity_type_definition = $this->entityTypeManager
->getDefinition($entity_type_id, FALSE);
if ($entity_type_definition
->hasKey('bundle')) {
$bundle_key = $entity_type_definition
->getKey('bundle');
$base_field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type_id);
$key_id = isset($base_field_definitions[$bundle_key]) ? $base_field_definitions[$bundle_key]
->getFieldStorageDefinition()
->getMainPropertyName() : 'value';
$bundle = isset($data[$bundle_key][0][$key_id]) ? $data[$bundle_key][0][$key_id] : (isset($data[$bundle_key]) ? $data[$bundle_key] : NULL);
}
$this
->decorateDenormalization($data, $entity_type_id, $format, $context);
$this
->fixReferences($data, $entity_type_id, $bundle);
$this
->cleanupData($data, $entity_type_id, $bundle);
$entity = parent::denormalize($data, $class, $format, $context);
$this
->decorateDenormalizedEntity($entity, $original_data, $format, $context);
return $entity;
}
public function normalize($object, $format = NULL, array $context = []) {
$normalized_data = parent::normalize($object, $format, $context);
$normalized_data['_content_sync'] = $this
->getContentSyncMetadata($object, $context);
$referenced_entities = $object
->referencedEntities();
if ($object
->getEntityTypeId() == 'menu_link_content') {
if ($entity = $this
->getMenuLinkNodeAttached($object)) {
$normalized_data['_content_sync']['menu_entity_link'][$entity
->getEntityTypeId()] = $entity
->uuid();
$referenced_entities[] = $entity;
}
}
if (!empty($referenced_entities)) {
$dependencies = [];
foreach ($referenced_entities as $entity) {
$reflection = new \ReflectionClass($entity);
if ($reflection
->implementsInterface(ContentEntityInterface::class)) {
$ids = [
$entity
->getEntityTypeId(),
$entity
->bundle(),
$entity
->uuid(),
];
$dependency = implode(ContentSyncManager::DELIMITER, $ids);
if (!$this
->inDependencies($dependency, $dependencies)) {
$dependencies[$entity
->getEntityTypeId()][] = $dependency;
}
}
}
$normalized_data['_content_sync']['entity_dependencies'] = $dependencies;
}
if (is_a($object, ContentEntityInterface::class, TRUE)) {
$this
->decorateNormalization($normalized_data, $object, $format, $context);
}
return $normalized_data;
}
protected function inDependencies($dependency, $dependencies) {
list($entity_type_id, $bundle, $uuid) = explode('.', $dependency);
if (isset($dependencies[$entity_type_id])) {
if (in_array($dependency, $dependencies[$entity_type_id])) {
return TRUE;
}
}
return FALSE;
}
protected function getMenuLinkNodeAttached(MenuLinkContent $object) {
$uri = $object
->get('link')
->getString();
$url = Url::fromUri($uri);
try {
$route_parameters = $url
->getRouteParameters();
if (count($route_parameters) == 1) {
$entity_id = reset($route_parameters);
$entity_type = key($route_parameters);
return \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($entity_id);
}
} catch (\Exception $e) {
}
}
public function supportsNormalization($data, $format = NULL) {
return parent::supportsNormalization($data, $format) && !empty($data->is_content_sync);
}
public function supportsDenormalization($data, $type, $format = NULL) {
return parent::supportsDenormalization($data, $type, $format);
}
protected function getContentSyncMetadata($object, $context = []) {
$metadata = [
'entity_type' => $object
->getEntityTypeId(),
];
return $metadata;
}
protected function getDecoratorManager() {
return $this->decoratorManager;
}
protected function fixReferences(&$data, $entity_type_id, $bundle = FALSE) {
if ($bundle) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
}
else {
$bundles = array_keys($this->entityTypeBundleInfo
->getBundleInfo($entity_type_id));
$field_definitions = [];
foreach ($bundles as $bundle) {
$field_definitions_bundle = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
if (is_array($field_definitions_bundle)) {
$field_definitions += $field_definitions_bundle;
}
}
}
foreach ($field_definitions as $field_name => $field_definition) {
if (!is_a($field_definition
->getClass(), '\\Drupal\\Core\\Field\\EntityReferenceFieldItemList', TRUE)) {
continue;
}
if (!empty($data[$field_name]) && is_array($data[$field_name])) {
$key = $field_definition
->getFieldStorageDefinition()
->getMainPropertyName();
foreach ($data[$field_name] as $i => &$item) {
if (!empty($item['target_uuid'])) {
$reference = $this->entityRepository
->loadEntityByUuid($item['target_type'], $item['target_uuid']);
if ($reference) {
$item[$key] = $reference
->id();
if (is_a($reference, RevisionableInterface::class, TRUE)) {
$item['target_revision_id'] = $reference
->getRevisionId();
}
}
else {
$reflection = new \ReflectionClass($this->entityTypeManager
->getStorage($item['target_type'])
->getEntityType()
->getClass());
if ($reflection
->implementsInterface(ContentEntityInterface::class)) {
unset($data[$field_name][$i]);
}
}
}
}
}
}
return $data;
}
protected function cleanupData(&$data, $entity_type_id, $bundle = FALSE) {
if ($bundle) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
}
else {
$bundles = array_keys($this->entityTypeBundleInfo
->getBundleInfo($entity_type_id));
$field_definitions = [];
foreach ($bundles as $bundle) {
$field_definitions_bundle = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
if (is_array($field_definitions_bundle)) {
$field_definitions += $field_definitions_bundle;
}
}
}
$field_names = array_keys($field_definitions);
foreach ($data as $field_name => $field_data) {
if (!in_array($field_name, $field_names)) {
unset($data[$field_name]);
}
}
}
}