You are here

public function KeywordNormalizer::denormalize in Bibliography & Citation 2.0.x

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

Overrides EntityNormalizer::denormalize

File

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

Class

KeywordNormalizer
Base normalizer class for bibcite formats.

Namespace

Drupal\bibcite_entity\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  $entity = parent::denormalize($data, $class, $format, $context);
  $entity_manager = $this
    ->getEntityTypeManager();
  if (!empty($context['keyword_deduplication'])) {
    $storage = $entity_manager
      ->getStorage('bibcite_keyword');
    $label_key = $storage
      ->getEntityType()
      ->getKey('label');
    $query = $storage
      ->getQuery()
      ->condition($label_key, trim($entity
      ->label()))
      ->range(0, 1);
    $ids = $query
      ->execute();
    if ($ids && ($result = $storage
      ->loadMultiple($ids))) {
      return reset($result);
    }
  }
  return $entity;
}