KeywordNormalizer.php in Bibliography & Citation 2.0.x        
                          
                  
                        
  
  
  
  
File
  modules/bibcite_entity/src/Normalizer/KeywordNormalizer.php
  
    View source  
  <?php
namespace Drupal\bibcite_entity\Normalizer;
use Drupal\serialization\Normalizer\EntityNormalizer;
class KeywordNormalizer extends EntityNormalizer {
  
  protected $supportedInterfaceOrClass = [
    'Drupal\\bibcite_entity\\Entity\\Keyword',
  ];
  
  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;
  }
}