You are here

public function ContributorNormalizer::denormalize in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Normalizer/ContributorNormalizer.php \Drupal\bibcite_entity\Normalizer\ContributorNormalizer::denormalize()

Overrides EntityNormalizer::denormalize

File

modules/bibcite_entity/src/Normalizer/ContributorNormalizer.php, line 22

Class

ContributorNormalizer
Base normalizer class for bibcite formats.

Namespace

Drupal\bibcite_entity\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {

  /** @var \Drupal\bibcite_entity\Entity\ContributorInterface $entity */
  $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;
}