You are here

KeywordNormalizer.php in Bibliography & Citation 8

Same filename and directory in other branches
  1. 2.0.x modules/bibcite_entity/src/Normalizer/KeywordNormalizer.php

File

modules/bibcite_entity/src/Normalizer/KeywordNormalizer.php
View source
<?php

namespace Drupal\bibcite_entity\Normalizer;

use Drupal\serialization\Normalizer\EntityNormalizer;

/**
 * Base normalizer class for bibcite formats.
 */
class KeywordNormalizer extends EntityNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var array
   */
  protected $supportedInterfaceOrClass = [
    'Drupal\\bibcite_entity\\Entity\\Keyword',
  ];

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

Namesort descending Description
KeywordNormalizer Base normalizer class for bibcite formats.