You are here

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

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

Overrides EntityNormalizer::denormalize

1 call to ReferenceNormalizerBase::denormalize()
BibtexReferenceNormalizer::denormalize in modules/bibcite_bibtex/src/Normalizer/BibtexReferenceNormalizer.php
2 methods override ReferenceNormalizerBase::denormalize()
BibtexReferenceNormalizer::denormalize in modules/bibcite_bibtex/src/Normalizer/BibtexReferenceNormalizer.php
CslReferenceNormalizer::denormalize in modules/bibcite_entity/src/Normalizer/CslReferenceNormalizer.php

File

modules/bibcite_entity/src/Normalizer/ReferenceNormalizerBase.php, line 165

Class

ReferenceNormalizerBase
Base normalizer class for bibcite formats.

Namespace

Drupal\bibcite_entity\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  $contributor_key = $this
    ->getContributorKey();
  if (!empty($data[$contributor_key])) {
    $contributors = (array) $data[$contributor_key];
    unset($data[$contributor_key]);
  }
  $keyword_key = $this
    ->getKeywordKey();
  if (!empty($data[$keyword_key])) {
    $keywords = (array) $data[$keyword_key];
    unset($data[$keyword_key]);
  }
  $type_key = $this
    ->getTypeKey();
  if (empty($data[$type_key])) {
    throw new UnexpectedValueException("Reference type is incorrect or not set.");
  }
  $reference_type = $data[$type_key];
  $converted_type = $this
    ->convertFormatType($reference_type, $format);

  // @todo Review logic of this exception because of $this->convertFormatType() always returns value.
  if (!$converted_type) {
    $url = Url::fromRoute('bibcite_entity.mapping', [
      'bibcite_format' => $format,
    ])
      ->toString();
    throw new UnexpectedValueException("'{$reference_type}' type is not mapped to reference type. <a href='{$url}'>Check mapping configuration</a>.");
  }
  unset($data[$type_key]);
  $data = $this
    ->convertKeys($data, $format);
  $data['type'] = $converted_type;

  /* @var \Drupal\bibcite_entity\Entity\Reference $entity */
  $entity = parent::denormalize($data, $class, $format, $context);
  if (!empty($contributors)) {
    $author_field = $entity
      ->get('author');
    foreach ($contributors as $name) {
      $author_field
        ->appendItem($this->serializer
        ->denormalize([
        'name' => [
          [
            'value' => $name,
          ],
        ],
      ], Contributor::class, $format, $context));
    }
  }
  if (!empty($keywords)) {
    $keyword_field = $entity
      ->get('keywords');
    foreach ($keywords as $keyword) {
      $keyword_field
        ->appendItem($this->serializer
        ->denormalize([
        'name' => [
          [
            'value' => $keyword,
          ],
        ],
      ], Keyword::class, $format, $context));
    }
  }
  return $entity;
}