ContributorNormalizer.php in Bibliography & Citation 8
File
modules/bibcite_entity/src/Normalizer/ContributorNormalizer.php
View source
<?php
namespace Drupal\bibcite_entity\Normalizer;
use Drupal\serialization\Normalizer\EntityNormalizer;
class ContributorNormalizer extends EntityNormalizer {
protected $supportedInterfaceOrClass = [
'Drupal\\bibcite_entity\\Entity\\ContributorInterface',
];
public function denormalize($data, $class, $format = NULL, array $context = []) {
$entity = parent::denormalize($data, $class, $format, $context);
$entity_manager = $this
->getEntityTypeManager();
if (!empty($context['contributor_deduplication'])) {
$storage = $entity_manager
->getStorage('bibcite_contributor');
$query = $storage
->getQuery()
->range(0, 1);
foreach ($entity::getNameParts() as $name_part) {
$value = $entity->{$name_part}->value;
if (!$value) {
$query
->notExists($name_part);
}
else {
$query
->condition($name_part, $value);
}
}
$ids = $query
->execute();
if ($ids && ($result = $storage
->loadMultiple($ids))) {
return reset($result);
}
}
return $entity;
}
}